0

I am using FusedLocationProviderClient to get the user current location, everything works fine but the first location that I am getting is shown every time I open the app , no matter where the phone is.

This method is giving me the current Latitude and Longitude

 private void currentCoordinate() {
    //getting the user last known location
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location)                        if (location != null) {
                        System.out.println(location.getLatitude() + "llloction");

                        getCurrentLocation(location.getLatitude(), location.getLongitude());
                    }
                }
            });
}

This is the method that getting the address from the Latitude and Longitude

private void getCurrentLocation(double latitude , double longitude  ) {

    Geocoder gCoder = new Geocoder(this);
    List<Address> addresses;

    try {
        addresses = gCoder.getFromLocation(latitude, longitude, 1);
        if (addresses != null && addresses.size() > 0){
            String stringForFragment = addresses + "";
            String test = stringForFragment;

            //split the address text for the first time
            test = test.
                    substring(test.indexOf("[addressLines=["),test.indexOf("],feature"));
            //split the adress text for the second time
            String finalTest = test.substring(test.indexOf(":") + 1);

            mPlaceAutocompleteFragment.setText(finalTest);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • 1
    Use location manager instead, because fused location return the last saved location. –  Jul 01 '18 at 21:37
  • @Ibrahim thank you I will try to do it, but can you tell me who is it possible for the location to be saved after the app got closed and opened again in another place? – Tamir Abutbul Jul 02 '18 at 19:31

0 Answers0