Google Maps API v3: How to remove all markers?

后端 未结 30 3314
悲哀的现实
悲哀的现实 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条回答
  •  温柔的废话
    2020-11-22 06:20

    A clean and easy application of rolinger's answer.

    function placeMarkerAndPanTo(latLng, map) {
          while(markersArray.length) { markersArray.pop().setMap(null); }
          var marker = new google.maps.Marker({
            position: latLng,
            map: map
          });
          map.panTo(latLng);
    
          markersArray.push(marker) ;
        }
    

提交回复
热议问题