Google Maps API v3: How to remove all markers?

后端 未结 30 3581
悲哀的现实
悲哀的现实 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:09

    I dont' know why, but, setting setMap(null) to my markers didn't work for me when I'm using DirectionsRenderer.

    In my case I had to call setMap(null) to my DirectionsRenderer as well.

    Something like that:

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    
    if (map.directionsDisplay) {
        map.directionsDisplay.setMap(null);
    }
    
    map.directionsDisplay = directionsDisplay;
    
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.TravelMode.DRIVING
    };
    
    directionsDisplay.setMap(map);
    directionsService.route(request, function (result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
        }
    });
    

提交回复
热议问题