Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear markers from Google Map in Android

I have added map on fragment activity and added several marker using addMarker function, but i am able to remove all markers , I am getting notification for different list of markers,

Now i wants to remove all markers and add new one.

one way to keep all markers in list and remove one by one, (marker.remove())

Is there any better way to clear all marker.

like image 808
Ashish Kasma Avatar asked Mar 27 '13 16:03

Ashish Kasma


People also ask

How do I remove markers from Google Maps?

When you use Google Maps to find a route to a destination, pins mark the starting point and the destination. Depending on the route, pins may also mark locations along the route, including detours. To remove any of these pins, right-click the Pin and select Remove this destination from the drop-down menu.

How do you remove map marks?

If you want to remove the pin from Google Maps, simply right click on it and select "Remove this destination." Poof, it's gone.


2 Answers

If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.

like image 177
CommonsWare Avatar answered Oct 11 '22 21:10

CommonsWare


If you do not wish to clear polylines and only the markers need to be removed follow the steps below.

First create a new Marker Array like below

List<Marker> AllMarkers = new ArrayList<Marker>();

Then when you add the marker on the google maps also add them to the Marker Array (its AllMarkers in this example)

for(int i=0;i<places.length();i++){

                LatLng location = new LatLng(Lat,Long);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(location);
                markerOptions.title("Your title");

                 Marker mLocationMarker = Map.addMarker(markerOptions); // add the marker to Map
                    AllMarkers.add(mLocationMarker); // add the marker to array

                }

then finally call the below method to remove all markers at once

 private void removeAllMarkers() {
        for (Marker mLocationMarker: AllMarkers) {
            mLocationMarker.remove();
        }
        AllMarkers.clear();

    }

call from anywhere to remove all markers

removeAllMarkers();

I found this solution when i was looking for a way to remove only the map markers without clearing the polylines. Hope this will help you too.

like image 7
FRANCIS FERNANDO Avatar answered Oct 11 '22 22:10

FRANCIS FERNANDO