OSMDroid覆盖多个静态和一个动态

时间:2012-02-23 16:48:56

标签: android osmdroid

在我的应用程序中,我已经在地图上有一些静态叠加层,以及一个动态,当GPS坐标改变时,它会改变它的位置。

我的问题是,当GPS坐标发生变化时,我必须清除动态叠加层并创建一个新的叠加层,但是当我这样做时,它会清除所有叠加层,使用:

mapView.getOverlays().clear();

所以,我正在努力寻找更好的方法。

清除所有叠加层,然后将它们重新放在地图上(消耗内存)或者我可以清除特定的叠加层吗?

由于

修改

这是动态标记:

@Override
public void onLocationChanged(Location location) {
    Log.d("Location", "onLocationChanged with location " + location.toString());

    mLatitude = (int) (location.getLatitude() * 1E6);
    mLongitude = (int) (location.getLongitude() * 1E6);

    GeoPoint gpt = new GeoPoint(mLatitude,mLongitude);

    markerYou.clear();
    markerYou.add(new OverlayItem(getString(R.string.markerYou), getString(R.string.markerYouDescription), gpt));

    mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(markerYou, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
        @Override
        public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
            Toast.makeText(ShowMap.this, getString(R.string.markerYouDescription), Toast.LENGTH_SHORT).show();
            return true;
        }
        @Override
        public boolean onItemLongPress(final int index, final OverlayItem item) {
            Toast.makeText(ShowMap.this, getString(R.string.markerYouDescription),Toast.LENGTH_SHORT).show();
            return true;
        }
    }, mResourceProxy);

    mapView.getOverlays().clear();
    mapView.getOverlays().add(mMyLocationOverlay);
    mapView.invalidate();

    mapController.setCenter(gpt);
}

这是放置静态标记的函数:

public void putPlacesOfInterest(){
    this.dh = new DataHelper(ShowMap.this);
    List<Pontos> list = this.dh.selectAll();
    for(Pontos p : list){
        markerPlaces.add(new OverlayItem(p.getName().toString(), p.getName().toString(), new GeoPoint(p.getLat(), p.getLng())));
    }
    mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(markerPlaces, new OnItemGestureListener<OverlayItem>() {

        @Override
        public boolean onItemLongPress(int index, OverlayItem item) {
            Toast.makeText(ShowMap.this, "Item " + item.mTitle, Toast.LENGTH_LONG).show();
            return true;
        }

        @Override
        public boolean onItemSingleTapUp(int index, OverlayItem item) {
            Toast.makeText(ShowMap.this, "Item " + item.mTitle, Toast.LENGTH_LONG).show();
            return true;
        }

    }, mResourceProxy);

    mapView.getOverlays().add(mMyLocationOverlay);
    mapView.invalidate();
}

3 个答案:

答案 0 :(得分:3)

真正的问题是你要添加mMyLocationOverlay有很多时间你调用mapView.getOverlays()。add(mMyLocationOverlay); 因此,实际上当您尝试清除mMyLocationOverlay时,您只清除1个实例。这意味着您可能有20个实例到mMyLocationOverlay。

只是想指出问题的真正原因,如果有人将来需要它。

答案 1 :(得分:1)

如果在添加动态叠加之前使用叠加列表的.size()方法 例如

int dynamicOverlayIndex = mapView.getOverlays().size()

然后,你可以删除那个:

mapView.getOverlays().remove(dynamicOverlayIndex);

答案 2 :(得分:0)

我使用这个解决方案解决了它:

Android MapView Can't Remove Marker

感谢NickT的帮助!