如何更新地图上显示的Android项目化叠加层

时间:2011-03-21 17:44:35

标签: android map location overlay

我有一个自定义叠加层,当用户(播放器)移动时,它应该移动。但是我现在拥有它的方式只是不断添加越来越多的叠加层,使得图标具有拖尾效果。

我尝试删除每个位置更新的叠加层,但似乎没有将其删除。虽然,我不确定是否真正删除它是完成我正在尝试做的正确方法。有没有办法只更新位置并刷新地图?

    public void drawMeOnMap()
{
    MapView mapView = (MapView) findViewById(R.id.mapView);
    mapOverlays = mapView.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.p18);
    itemizedOverlay = new IOverlay(drawable);

    if (mapOverlays.contains(itemizedOverlay))
    {
        mapOverlays.remove(itemizedOverlay);
    }

    GeoPoint point = new GeoPoint((int)(1E6*player.latitude), (int)(1E6*player.longitude));
    OverlayItem item = new OverlayItem(point, "", "");
    itemizedOverlay.addOverlay(item);
    mapOverlays.add(itemizedOverlay);
}

非常感谢任何指导。

1 个答案:

答案 0 :(得分:14)

为什么不使用MyLocationOverlay?它会为你完成这一切。只需确保在onPause中调用disableMyLocation()

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();

在任何情况下,您都不需要每次都获得Mapview,mapOverlays,drawable和创建新的itemizedOverlay。这应该只在你的onCreate或其他任何一次。你应该做的只是在位置改变时更新OverlayItem,然后调用mapView.invalidate()来重绘视图。

GeoPoint point = new GeoPoint((int)(1E6*player.latitude), (int)(1E6*player.longitude));
OverlayItem item = new OverlayItem(point, "", "");
itemizedOverlay.clear();
itemizedOverlay.addOVerlay(item);
mapView.invalidate();
相关问题