-4

I have this method to get the user location and I want to return myLocation variable :

mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                LatLng mylocation = new LatLng(latitude, longitude);
            }
        }
    });

I have this method inside of a function that returns a LatLng value but I can't get the value from inside of the method.

Casimiro
  • 25
  • 8

1 Answers1

-1

Declare the Fused Location Client class

private Location currentLocation; 

public Location getCurrentLocation() {
    return currentLocation;
}

mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                 this.currentLocation= location;
            }
        }
    });

after getCurrentLocation() that process the your current location LatLng

        if (getCurrentLocation()  != null) {
            double latitude = getCurrentLocation() .getLatitude();
            double longitude = getCurrentLocation() .getLongitude();

            LatLng mylocation = new LatLng(latitude, longitude);
        }
Manikandan K
  • 911
  • 9
  • 21