MyLocationOverlay disappear problem next time loading activity?
friends,
i have noticed one thing when my location is same MyLocationOverlay marker is not displayed on screen.
i have tested it when i change emulator location values it is appeared on screen i go to next page then come to same page again and it is not shown.yes when i change location values it is appeared again. i want to show it always if location is changed or not any one guide me how?
here is my code
onCreate()
{
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
mapView.postInvalidate();
zoomToMyLocation()开发者_开发问答;
}
@Override
protected void onPause() {
super.onPause();
if (myLocOverlay != null)
myLocOverlay.disableMyLocation();
}
@Override
public void onResume() {
if (myLocOverlay != null)
myLocOverlay.enableMyLocation();
super.onResume();
}
private void zoomToMyLocation() {
GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
if(myLocationGeoPoint != null) {
mapView.getController().animateTo(myLocationGeoPoint);
mapView.getController().setZoom(10);
}
else {
Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
}
}
If you are calling finish() method while calling intent (means your other page) then you are destroying all the values in your page and moving towards new page.
example Activity A Activity B
moving from Activity A to Activity B
Intent intent = new Intent();
intent.setclass(...);
startActivity(intent);
coming back to Activity A
just call finish() on Activity B
Actually the reason was.
first time when i load record it get gps location on the spot. sometimes when i come to this activity gps take time to obtain location and my code is passed and nothing is appeared.
so problem is if location is not obtained intime then i should wait for it and display as we get it.
so i implemented RunOnFirstFix() to get rid of this issue. and another most important thing use Mapview.postInvalidate() to display it on mapview.
so if we follow this step we wont be having MyLocationOverlay disappear problem.
code sample onCreate()
{
// new line
myLocOverlay.runOnFirstFix(showCurrentLocationIfObtainedLate );
}
Runnable showCurrentLocationIfObtainedLate = new Runnable() {
@Override
public void run() {
if(myLocOverlay.getMyLocation() != null)
{
mapView.getController().animateTo(myLocOverlay.getMyLocation());
mapView.postInvalidate();
}
}
};
精彩评论