Refresh overlay object in Map Activity on Android

10,827

Solution 1

mapView.postInvalidate() will update the map view and redraw the overlays. I'm using this with a Runnable to refresh my overlays every second:

private Handler handler = new Handler();

private Runnable refreshTask = new Runnable()
{
  public void run()
  {
    handler.removeCallbacks(this);

    mapView.postInvalidate();

    handler.postDelayed(this, 1000);

  }
};

Solution 2

That might not be perfect but it worked for me, you may move the map a bit forth and back, which updates all overlays afterwards.

mapView.getController().scrollBy(1, 1);
mapView.getController().scrollBy(-1, -1);
Share:
10,827
aladine
Author by

aladine

Updated on June 05, 2022

Comments

  • aladine
    aladine about 2 years

    I have to build a map activity and on top of that, I need to display some location points. These locations are retrieving from a hashtable in another class. The content of this hashtable always changes. So I expect to see those location spots on the Map Activity move as the hashtable is modified. The code is shown below:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
    
        MapView mapView = (MapView) this.findViewById(R.id.map);   
        mapView.setStreetView(true);
        mc = mapView.getController();
    
    
        mc.animateTo(...);
        mc.setZoom(12); 
    
        mapView.invalidate();
    
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
    
        MarkerOverlay mark = new MarkerOverlay(p);
         listOfOverlays.add(mark);
    
        for(Entry<Long,Pair> p: LQTManager.getInstance().ResultTable.entrySet())
        {
    
            GeoPoint point = new GeoPoint(
                        (int) (p.getValue().x * 1E6), 
                        (int) (p.getValue().y * 1E6));//some code is not shown
             MarkerOverlay markerOverlay = new MarkerOverlay(point);
             listOfOverlays.add(markerOverlay); 
    
    
    
        }
    
        mapView.postInvalidate();
    
    
    }
    
    
    class MarkerOverlay extends com.google.android.maps.Overlay
    {
        public GeoPoint pt;
    
        public MarkerOverlay(GeoPoint pt){
            this.pt=pt;
            }
    
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
    
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pt, screenPts);
    
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.pushpin);            
    
            canvas.drawBitmap(bmp, screenPts.x-16, screenPts.y-32, null); 
    
            return true;
        }
    } 
    

    Is there any way to refresh overlay objects if I touch the screen. I am thinking about put some functions in onTap override method of MarkerOverlay. But I think is is not logically if I try to make the Overlay try to clear itself? Any suggested idea?

  • Martin
    Martin almost 12 years
    I think that only applies to ItemizedOvelays. Aladine is overriding Overlay directly.
  • Martin
    Martin almost 12 years
    Surely that will jitter the display on lower dpi devices?
  • Martin
    Martin almost 12 years
    Perfect - thanks. @hectichavana - If you don't want periodic updates, just call mapView.postInvalidate() directly, and ignore all the Runnable stuff. For the periodic updates, see developer.android.com/reference/android/os/Handler.html.
  • joecks
    joecks almost 12 years
    Yes it will, so I would recommend the answer of filitchp!