我在我的项目中使用osmdroid API进行地图。
下面是我写的在所选位置放置叠加(标记)的代码。
如果我点击地图选择任何位置,它会在那里放置一个叠加层,但如果我再次点击地图选择任何新位置,它会在那里显示一个新的叠加层(它应该),但它不会t从先前位置删除叠加层。
因此,如果我选择10个位置,则会显示10个叠加层!
我的问题是,如何在选择新位置时删除以前放置的叠加层?
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int actionType = ev.getAction();
switch (actionType) {
case MotionEvent.ACTION_UP:
Projection proj = mMapView.getProjection();
IGeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY());
String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
GeoPoint mypointicon = new GeoPoint(loc.getLatitude(), loc.getLongitude());
final ArrayList<OverlayItem> items=new ArrayList<>();
items.add(new OverlayItem("Here", "Sample Description", mypointicon));
this.mMyLocationOverlay = new ItemizedIconOverlay<>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
return true;
}
@Override
public boolean onItemLongPress(final int index, final OverlayItem item) {
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
Toast.makeText(getApplicationContext(),
"Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG).show();
}
return super.dispatchTouchEvent(ev);
}
答案 0 :(得分:1)
基本上,您必须管理地图叠加层。
你可以添加一个叠加:this.mMapView.getOverlays()。add(this.mMyLocationOverlay);
所以你也可以删除它:this.mMapView.getOverlays()。remove(index);
你可以删除所有这些:this.mMapView.getOverlays()。clear();