-1

Is it possible to clear all the google maps markers EXCEPT the selected one (the one displaying an info window), and keep its info window open?

I'm using this code to refresh my map, it is clearing all the markers and add new ones according to where the camera is :

            googleMap.clear();
            // Save all the marker which will be kept on a new list, remove the others

            for (int j = 0; j < markers.size(); j++) {
                googleMap.addMarker(markers.get(j));
            }

But as the info windows are clickable and on click open a new fragment, I want the selected marker to be spared from this clear(), so the marker and its info window don't disappear. I would like to achieve this by using the method provided in the google map object rather than loop on a list of markers, which can take a lot of time.

Thanks.

Virthuss
  • 3,142
  • 1
  • 21
  • 39

2 Answers2

1

I guess, you should use, map.clear() to remove all markers from the map. And then as per the @Hiren commented, you can add a marker which you wanted to preserve. This will be little faster that what you're expecting.

Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title"));

Note: map.clear(); will also remove Polylines, Circles etc.

Hemang
  • 26,840
  • 19
  • 119
  • 186
0

Can you please try this one ?

Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title"));

Whenever you want to remove it, you can call

markerName.remove();

If you have multiple data then work with for loop and remove all except your selected marker.

Hope this will make sense.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • Hi Hiren, it does sense yes, I was think about using it. However I was more preciesly looking for a way to achieve this using the method provided in my google map object, for more clarity – Virthuss Jan 20 '16 at 07:35
  • @Virthuss, okay please try it – Hiren Patel Jan 20 '16 at 07:35
  • I already implemented it, it works. But looping like this on a set of markers have bad performances. Thats why I'm looking for another way – Virthuss Jan 20 '16 at 07:37