1

I searched everywhere on the Internet for a good tutorial that explains how to get the user's location on the MapsActivity (Android Studio). But I didn't find, even some explanations in a forum.

So here's what I want: When the user goes on the Map layout of my app, they should see their current location.

I created the MapsActivity (from Android Studio) and put the right permissions on my Manifest. But I do not know what function to use to get the current user's location and where to put that function?

Below is the code of the MapsActivity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Paris and move the camera
        LatLng paris = new LatLng(48.858930, 2.350831);
        mMap.addMarker(new MarkerOptions().position(paris).title("Marker in Paris"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(paris));
    }
}

Thank you very much!

buczek
  • 2,011
  • 7
  • 29
  • 40
  • Have you tried something. You can try as written in [this answer](http://stackoverflow.com/questions/21403496/how-to-get-current-location-in-google-map-android)! – Nav Apr 15 '16 at 20:28
  • @iOSDeveloper the accepted answer there is deprecated. For api-23 you also need to request the Location permission from the user at runtime, see here: [How can I use Google Maps and LocationManager to show current location on Android M?](http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi) – Daniel Nugent Apr 15 '16 at 22:14
  • Thank you very much ! It works :) – Julien Elkhiati Apr 16 '16 at 13:20

1 Answers1

0

Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready. The following code snippet illustrates the request and a simple handling of the response:

This will give you the last location. Then you can move the camera to here with your code above. More info in http://developer.android.com/training/location/retrieve-current.html.

public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
...
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
        mGoogleApiClient);
if (mLastLocation != null) {
    mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
    mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
    }
  }
}
jgm
  • 1,230
  • 1
  • 19
  • 39