I'm using osmdroid API for maps in my project.
Below is the code I wrote to put an overlay(marker) on selected location.
If I click on map to select any location, it puts an overlay there, but if I tap on map again to select any new location, it shows a new overlay over there (it should) but it doesn't remove the overlay from previous location.
So if I select 10 locations, it shows 10 overlays!
My question is, how to remove previously put overlays when a new location is selected?
@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);
}
Basically, you have to manage your map overlays.
You could add an overlay: this.mMapView.getOverlays().add(this.mMyLocationOverlay);
So you also can remove it: this.mMapView.getOverlays().remove(index);
And you can remove all of them: this.mMapView.getOverlays().clear();