How to get your own location (LatLng) in Android Studio [duplicate]

1

I need to set my own location on my map to create the route from where I am to the point I want to reach. I can create a bookmark for the destination but I do not know how to request the android my own location and bookmark on the map.

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

    
asked by anonymous 07.02.2018 / 20:39

1 answer

0

Assuming you're already using google play services and have 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()));
    }
  }

The documentation also has the example of getting your own location too: Android Developers

    
07.02.2018 / 21:12