Search code examples
androidandroid-mapviewmylocationoverlay

mylocationoverlay disappears first time it find location


I am trying to setup a mylocationoverlay. Unfortunately, it is acting quite strangely. It works fine, except it does not appear until after I leave the MapActivity and come back in my application. Initially the map appears and there is a blue circle while it is getting a fine location. However, instead of resolving to a point, the circle just disappears.

My Code looks like this:

onResume() {
    myLocation = new MyLocationOverlay(getActivity(), mp);
myLocation.enableMyLocation();
myLocation.runOnFirstFix(new Runnable(){
       public void run() {
          map.getOverlays().clear();
      map.getOverlays().add(myLocation);
      map.postInvalidate();
       }
    }
}


onPause() {
  myLocation.disableMyLocation();
  layout.removeView(map);
  map = null;

}

Does anyone have any thoughts on what might be happening here? Since this is pretty much verbatim what all the examples online look like, I might add that I am testing this on a motorolla atrix running 2.3.4.


Solution

  • Edit : Let me take you through your code:

    onResume() {
    // First time: draw a circle somewhere here. There is no GPS fix yet, so no dot. 
    // Second time: The dot from the previous fix exists, so you get a circle and dot.
    myLocation = new MyLocationOverlay(getActivity(), mp);
    myLocation.enableMyLocation();
    myLocation.runOnFirstFix(new Runnable(){
           public void run() {
           // First time: removes the circle and draws a dot.
           // Second time: removes the circle and dot, and draw a new dot. 
           map.getOverlays().clear();
           map.getOverlays().add(myLocation);
           map.postInvalidate();
           }
        }
    }
    

    map.getOverlays().clear(); removes the circle

    use remove() instead to remove the overlay(s) that you don't want, instead of clearing them all.

    Remember to call map.invalidate(); whenever you need to force a redraw