Google Maps API v3: How to remove all markers?

后端 未结 30 2897
悲哀的现实
悲哀的现实 2020-11-22 05:36

In Google Maps API v2, if I wanted to remove all the map markers, I could simply do:

map.clearOverlays();

How do I do this in Google Maps A

30条回答
  •  旧时难觅i
    2020-11-22 06:18

    I found using markermanager library in the google-maps-utility-library-v3 project as the easiest way.

    1. Set up the MarkerManager

    mgr = new MarkerManager(map);
    google.maps.event.addListener(mgr, 'loaded', function () {
        loadMarkers();
    });
    

    2. Add markers to the MarkerManager

    function loadMarkers() {
      var marker = new google.maps.Marker({
                title: title,
                position: latlng,
                icon: icon
       });
       mgr.addMarker(marker);
       mgr.refresh();
     }
    

    3. To clear markers you just need to call the MarkerManger's clearMarkers() function

    mgr.clearMarkers();
    

提交回复
热议问题