Google Maps API v3: How to remove all markers?

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

    Simply do the following:

    I. Declare a global variable:

    var markersArray = [];
    

    II. Define a function:

    function clearOverlays() {
      for (var i = 0; i < markersArray.length; i++ ) {
        markersArray[i].setMap(null);
      }
      markersArray.length = 0;
    }
    

    OR

    google.maps.Map.prototype.clearOverlays = function() {
      for (var i = 0; i < markersArray.length; i++ ) {
        markersArray[i].setMap(null);
      }
      markersArray.length = 0;
    }
    

    III. Push markers in the 'markerArray' before calling the following:

    markersArray.push(marker);
    google.maps.event.addListener(marker,"click",function(){});
    

    IV. Call the clearOverlays(); or map.clearOverlays(); function wherever required.

    That's it!!

    0 讨论(0)
  • 2020-11-22 06:16

    It seems that there is no such function in V3 yet.

    People suggest to keep references to all markers you have on the map in an array. And then when you want to delete em all, just loop trough the array and call .setMap(null) method on each of the references.

    See this question for more info/code.

    My version:

    google.maps.Map.prototype.markers = new Array();
    
    google.maps.Map.prototype.getMarkers = function() {
        return this.markers
    };
    
    google.maps.Map.prototype.clearMarkers = function() {
        for(var i=0; i<this.markers.length; i++){
            this.markers[i].setMap(null);
        }
        this.markers = new Array();
    };
    
    google.maps.Marker.prototype._setMap = google.maps.Marker.prototype.setMap;
    
    google.maps.Marker.prototype.setMap = function(map) {
        if (map) {
            map.markers[map.markers.length] = this;
        }
        this._setMap(map);
    }
    

    The code is edited version of this code http://www.lootogo.com/googlemapsapi3/markerPlugin.html I removed the need to call addMarker manually.

    Pros

    • Doing this way you keep the code compact and in one place (doesn't pollute the namespace).
    • You don't have to keep track of the markers yourself anymore you can always find all the markers on the map by calling map.getMarkers()

    Cons

    • Using prototypes and wrappers like I did now makes my code dependent on Google code, if they make a mayor change in their source this will break.
    • If you don't understand it then you won't be able to fix it if does break. The chances are low that they're going to change anything which will break this, but still..
    • If you remove one marker manually, it's reference will still be in markers array. (You could edit my setMap method to fix it, but at the cost of looping trough markers array and removing the reference)
    0 讨论(0)
  • 2020-11-22 06:16

    Here you can find an example of how to remove markers:

    https://developers.google.com/maps/documentation/javascript/examples/marker-remove?hl=es

    // Add a marker to the map and push to the array.
    function addMarker(location) {
      var marker = new google.maps.Marker({
        position: location,
        map: map
      });
      markers.push(marker);
    }
    
    // Sets the map on all markers in the array.
    function setAllMap(map) {
      for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(map);
       }
    }
    
    // Removes the markers from the map, but keeps them in the array.
    function clearMarkers() {
      setAllMap(null);
    }
    
    // Deletes all markers in the array by removing references to them.
    function deleteMarkers() {
      clearMarkers();
      markers = [];
    }
    
    0 讨论(0)
  • 2020-11-22 06:17

    Same problem. This code doesn't work anymore.

    I've corrected it, change clearMarkers method this way:

    set_map(null) ---> setMap(null)

    google.maps.Map.prototype.clearMarkers = function() {
        for(var i=0; i < this.markers.length; i++){
            this.markers[i].setMap(null);
        }
        this.markers = new Array();
    };
    

    Documentation has been updated to include details on the topic: https://developers.google.com/maps/documentation/javascript/markers#remove

    0 讨论(0)
  • 2020-11-22 06:18

    The "set_map" function posted in both answers appears to no longer work in Google Maps v3 API.

    I wonder what happened

    Update:

    It appears Google changed their API such that "set_map" is not "setMap".

    http://code.google.com/apis/maps/documentation/v3/reference.html

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题