Regarding current location in android [duplicate]
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){
lastLocati开发者_StackOverflow中文版on = _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:
- Make a AsyncTask.
- In preExecute creates a dialog or does something that shows the user that you are looking for the current location.
- In backgroundProccess gets a new location.
- In postExecute inform the user with the new(current) location.
- When you press your button run the AsyncTask.
精彩评论