开发者

How to update markers on the MapView?

开发者 https://www.devze.com 2023-03-18 16:20 出处:网络
I\'ve added a few markers to my MapView: List<Overlay> mapOverlays; Drawable drawable; MapOverlay itemizedOverlay;

I've added a few markers to my MapView:

List<Overlay> mapOverlays;
Drawable drawable;
MapOverlay itemizedOverlay;

mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.map_dot_green);
itemizedO开发者_Go百科verlay = new MapOverlay(drawable, mapView);
GeoPoint markerPoint = new GeoPoint(lat, lng);
OverlayItem overlayItem = new OverlayItem(markerPoint, "name", "description");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);

Now I need to change markers (drawables) regularly for them. I am planning to use Runnable for this. But how can I replace the markers?

Upd. here is the current code for the same:

public class Map extends MapActivity {
    private MapOverlay itemizedOverlay;
    private MapView mapView;

private Runnable mUpdateMapMarkers = new Runnable() {
     public void run() {
       Drawable drawable_green = getResources().getDrawable(R.drawable.map_dot_green);
       int w = drawable_green.getIntrinsicWidth();
       int h = drawable_green.getIntrinsicHeight();
       drawable_green.setBounds(-w / 2, -h, w / 2, 0);

       for (int i = 0; i < itemizedOverlay.size(); i++) {
         itemizedOverlay.getItem(i).setMarker(drawable_green);
       }
       mapView.invalidate();

         mHandler.postDelayed(mUpdateMapMarkers, 5*60*1000); // once in 5 minutes   
     }
  };


Either use LevelListDrawables and change the level, or create your own OverlayItem subclass and override getMarker(). This sample project demonstrates the latter approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消