Possible Duplicate:
GPS not update location after close and reopen app on android
I know that question is asked already but my problem is that.
every time when I open my app it shows last location not current location
I have a button for current location and i want when i open my app it show current location on map after then when I click on button then it show current location.
so my problem is when i open my app it shows last location but when i press then it shows current location.
code in onCreate()
_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
_locationListener = new CurrentLocationListener(this,this);
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,_locationListener );
_currentGeoPoint = getKnownLocation();
_currentPosOverlay = new OverlayItem(_currentGeoPoint, "", "");
Log.d(" overlay", "overlaylast "+_currentGeoPoint);
// Toast.makeText(getBaseContext(), "overlay12333vvv "+_currentGeoPoint, Toast.LENGTH_SHORT).show();
_customOverlay.addOverlay(_currentPosOverlay);
_mapOverlays.add(_customOverlay);
Code for getKnownLocation()
// TODO Auto-generated method stub
Location lastLocation = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastLocation == null){
lastLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(lastLocation != null){
GeoPoint gp = new GeoPoint((int)(lastLocation.getLatitude() * 1e6), (int)(lastLocation.getLongitude() * 1e6));
last_loc=gp;
return gp;
}else {
return _mapView.getMapCenter();
}
For the button I called getKnownLocation()
Of cause it shows your last know location, thats what your code does.
You need to give the LocationManager some time to find your current location, you can’t expect it to know it without calculating it.
What you can do is: