4

I'm creating an Android Application where i need to set markers on the map. And since my App requires Offline function i need to use Osmdroid to solve this problem. Now my issue is the markers on the map, which i can easily add them by using Markers or the ItemizedOverlay, but the issue i'm having is that i cannot remove markers from the map.

The code i have used for adding markers is this one:

    Marker marker = new Marker(mapView);
    marker.setPosition(new GeoPoint(41.3746312,19.7710733));
    marker.setIcon(getResources().getDrawable(R.drawable.marker));
    marker.setImage(getResources().getDrawable(R.drawable.marker));
    marker.setTitle("Marker");
    marker.setInfoWindow(null);
    marker.showInfoWindow();
    mapView.getOverlays().add(marker);
    mapView.invalidate();

but i'm encountering issues on removing them since the only way to remove it is:

    mapView.getOverlays().clear();

And i need to remove a specific marker instead of all of them at the same time.

Kristi Leka
  • 41
  • 1
  • 6

4 Answers4

4

For removing specific marker you should use:

    mapView.getOverlays().remove(overlay);
    mapView.invalidate();
ivanovd422
  • 357
  • 5
  • 18
3
  1. When you add a marker set the Id to anything you like.

     MapView mapview = (MapView) findViewById(R.id.mapview);
     Marker marker = new Marker(mapview);
     marker.setId("String");
     mapview.getOverlays().add(marker);
    
  2. Since a Marker will get added to the mapviews overlay list as an Overlay object you can loop throgh it until you find your specific marker based on the Id.

     for(int i=0;i<mapview.getOverlays().size();i++){
        Overlay overlay=mapview.getOverlays().get(i);
        if(overlay instanceof Marker&&((Marker) overlay).getId().equals("String")){
           mapview.getOverlays().remove(overlay);
      }
    }
    
  • Depending on the version of your `bonus pack`, you might need to use the `setTitle` and `getTitle` methods. – tony gil Sep 01 '21 at 17:04
0

There is Another way of adding and removing items.

first, you need this snippet :

ArrayList<OverlayItem> items = items = new ArrayList<OverlayItem>();
ItemizedOverlayWithFocus<OverlayItem> mOverlay = mOverlay = new ItemizedOverlayWithFocus<OverlayItem>(items,
            new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                @Override
                public boolean onItemSingleTapUp(final int position, final OverlayItem item) {
                    //do something
                    return true;
                }
                @Override
                public boolean onItemLongPress(final int position, final OverlayItem item) {
                    return false;
                }
            }, getApplicationContext());
 mOverlay.setFocusItemsOnTap(true);

to add items before app starting, use this code :

mOverlay.addItem(new OverlayItem("Title", "Description", new 
GeoPoint(31.7359474,55.4384632))); 

map.getOverlays().add(mOverlay);

while running the app you can add more items with below code :

mOverlay.addItem(new OverlayItem("new Title", "new Description", new GeoPoint(Latitude,Longitude)));

removing items while the app is running with below code :

mOverlay.removeItem(int position);

the position is the n-th item you added to list.you can put remove code into onItemSingleTapUpthat we use up in this answer, that means when you tap on item it will remove from screen.

0

Kotlin version:

val gPt = GeoPoint(location.latitude, location.longitude)
val marker = Marker(map)
marker.position = gPt
marker.icon = getDrawable(R.drawable.ic_baseline_location_on_24)
marker.id = "String"
map.overlays.add(marker)
map.invalidate()

To remove (adapted from Dren Pirraku's answer):

map.overlays.forEach {
  if (it is Marker && it.id == "String") {
    map.overlays.remove(it)
  }
}
iamfredrik
  • 340
  • 1
  • 6
  • 15