0

I've got function which retutrns my current location.

public LatLng getCurrentLocation() {

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = service.getBestProvider(criteria, false);

        Location location = service.getLastKnownLocation(provider);
        LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
        //Log.e("Location",userLocation.latitude+ " x "+ userLocation.longitude);

        return userLocation;

    }

The problem is that when I moved to another location (50 km) I still returns previous location. Any ideas why is it so?

  • please check this answear: [http://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android](http://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android) – Victor Gomes Jun 17 '16 at 09:47

1 Answers1

0

Use onLocationChanged instead your function, you could try this simple example that i'm using :

@Override
public void onLocationChanged(Location location) {

    // TextView to show your current location in your activity
    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    Log.i("Current position", String.valueOf(latLng));

    // add marker to the current position
    googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .draggable(true)
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng   ));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

}
BOUTERBIAT Oualid
  • 1,494
  • 13
  • 15