removing specific overlays from mapview

3k Views Asked by At

my problem is as follows.

I am creating multiple itemized overlays. (because every overlay gets a different drawable) I customized the itemized overlay class, but when i add it to the mapview overlays, the class is transformed into an overlay class. to make it worse i got 3 classes creating overlays on the same map. each class represents an item on the map with it's own intelligence behind it.

the problem i now have is that i want to remove an overlay, but i can not be sure that the index i inserted it on, is also the index it has when i try to remove it. (the other classes might have inserted an overlay in the mean time)

the classes are self updating, so i do not want a solution that fires an update or delete event from the main class. (the whole point is to add a class and forget about it)

so my question would be: how can i identify which layer is which when i want to call a remove on that layer. i think the information is available, but i do not know how to get to it. this is the code i am using to add the overlay

 OverlayItem overlayitem = new OverlayItem(p,myNaam ,myOmschrijving );

    LocationOverlay = new MyLocationOverlay(drawable, myContext);

    LocationOverlay.SetLocation(i,overlayitem);

    myOverlays.add(LocationOverlay);
2

There are 2 best solutions below

4
On

You don't have to remove specific layer. You can remove an overlay specified by it's reference (e.g. myOverlay).

LocationOverlay myOverlay = new MyLocationOverlay(drawable, myContext); //`you forgot the name of variable`

mapView.getOverlays().remove(myOverlay);
0
On

You can set a certain integer as a position for every overlays something like that :

mapView.getOverlays().add(0,myScaleBarOverlay);

and when you want ro remove this call:

mapView.getOverlays().remove(0);
mapView.invalidate();

Regard