How to get your own location (Latlng) in Android Studio

Asked

Viewed 1,510 times

1

I need to set my own location on my map to calculate the route from where I am to where I want to go. I can create a marker for the destination, but I do not know how to ask android my own location and mark on the map.

I know you need to mMap.setMyLocationEnabled(true); but without the LatLng I can’t position or mark on the map.

1 answer

0

Suppose you are already using google play services and already connected (example below):

 if (mGoogleApiClient == null) {
     mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();
 }
 if (mGoogleApiClient != null) {
    mGoogleApiClient.connect();
 }

Now it’s just:

  @Override
  public void onConnected(Bundle connectionHint) {
    myLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (myLocation != null) {
       latText.setText(String.valueOf(myLocation.getLatitude()));

       longText.setText(String.valueOf(myLocation.getLongitude()));
    }
  }

Even, in the documentation has the example of how to get your own location also: Android Developers

  • I’m using Play service:11.8.0. But when I write Locationservices it doesn’t recognize

Browser other questions tagged

You are not signed in. Login or sign up in order to post.