GetLastKnownLocation () always returns the same location

I try to get the location from gps / netwrok by the best provider, but it always returns the same location, also I see that there is a sign on my Google map in my right place. please can someone tell me what i am doing wrong?

My activity:

public class MainMenu2 extends Activity implements LocationListener, OnClickListener 

on my onCreate I have:

  if(isGooglePlay()){ setContentView(R.layout.menu_2); setUpMapIfNeeded(); } 

isGooglePlay method:

  private boolean isGooglePlay() { // looks if googlemaps is available int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if(status == ConnectionResult.SUCCESS){ return true; } else{ ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show(); //Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show(); return(false); } }//isGooglePlay 

setUpMapIfNeeded method:

  private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.menu_map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { // The Map is verified. It is now safe to manipulate the map. mMap.setMyLocationEnabled(true); locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(ACCURACY_FINE); provider = locationManager.getBestProvider(criteria, true); if (provider == null){ onProviderDisabled(provider); } locationManager.requestLocationUpdates(provider, 100, 1, this); loc = locationManager.getLastKnownLocation(provider); //---here the privider is "gps" but always the same location---// if (loc != null){ onLocationChanged(loc); } mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // set Map Type } } }//setUpMap 

and override the methods:

  @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude()); mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(10)); double pLong = location.getLongitude(); double pLat = location.getLatitude(); Latstr = String.valueOf(pLong); Lngstr = String.valueOf(pLat); latitude = pLat; longitude = pLong; } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub Toast.makeText(this, "Please turn ON the GPS", Toast.LENGTH_SHORT).show(); } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override protected void onResume() { super.onResume(); myLatLng = new LatLng(latitude,longitude); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLatLng, 15); mMap.animateCamera(update); mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude )).title("Find me here!")); } 

I also tried to find the similer problem here, but I got nothing.

+7
source share
1 answer

There is an error with LocationProvider on some platforms, such as Samsung, Android custom build. You must call this code (which actually does nothing), which causes the phone's background cache to update the current location.

If you do not believe me, change your GPS position to another place about 200 meters, download the application, you will not notice that the GPS location has changed. Now just download the Maps application to your phone, and suddenly your application will show your current location.

To do this, trigger the programmatically place these lines immediately before calling getLastKnownLocation .

 HomeScreen.getLocationManager().requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(final Location location) { } }); 

Edit

OR

Use the new api LocationClient instead of LocationProvider.

+7
source

All Articles