删除标记组Google Maps API v3

时间:2013-05-08 08:44:33

标签: google-maps-api-3 overlay google-maps-markers

我希望在地图上查看和删除标记,具体取决于用户点击位置类型..

我正在使用此代码删除标记

1-将markersArray定义为全局数组

 var markersArray =new Array();

2-定义clearOverlays函数

function clearOverlays() {


   if ( markersArray &&  markersArray.length) {


    google.maps.Map.prototype.clearOverlays = function() {

        for (var i = 0; i <  markersArray.length; i++ ) {

            markersArray[i].setMap(null);
        }
    } 

     markersArray = [];
     markersArray = new Array();
     markersArray.length = 0;
  }

}

3-当我想删除标记时,我会调用该函数

问题是:没有从地图中删除任何内容!

我想知道为什么!

编辑:我在这里添加标记并将其推入markersArray

  downloadUrl("getPlacesForType/"+markerType+"/"+lang, function(data) {


                var xml = data.responseXML;

                var myMarkersArray = xml.documentElement.getElementsByTagName("marker");


                for (var i = 0; i < myMarkersArray.length; i++) {

                    var name = myMarkersArray[i].getAttribute("name");
                    var version = myMarkersArray[i].getAttribute("version");
                    var description = myMarkersArray[i].getAttribute("description");
                    var type = myMarkersArray[i].getAttribute("type");
                    var point = new google.maps.LatLng(
                        parseFloat(myMarkersArray[i].getAttribute("lat")),
                        parseFloat(myMarkersArray[i].getAttribute("lng")));
                    var html = "<b>" + name + "</b> <br/>" + description;
                    var icon = customIcons[type] || {};

                    var marker = new google.maps.Marker({
                        map: map,
                        position: point ,
                        icon: icon.icon,
                        shadow: icon.shadow
                    });
                    markersArray.push(marker);
                    bindInfoWindow(marker, map, infoWindow, html);
                } 
            }); 

1 个答案:

答案 0 :(得分:1)

尝试使用数组来存储标记。你需要在创建它时按下每个标记。

var markerArray=[];
var locations = [ [56.066877,21.069274], [55.279849,26.048155] ];
for (i = 0; i < locations.length; i++) 
{
 marker = new MarkerWithLabel({
 position: new google.maps.LatLng(locations[i][0], locations[i][1]), 
 map: map 
 });
markerArray.push(marker);
} /

并删除标记,

function deleteMarkers() {
  if (markersArray) {
  for (i in markersArray) {
   markersArray[i].setMap(null);
  }
markersArray.length = 0;
}
}

看看这个小例子,

    <!DOCTYPE html>
<html>
<head>

 <title>Google Maps</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    var markersArray = [];
    function initialize() {
        var mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(-25.363882, 131.044922),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);

        google.maps.event.addListener(map, 'click', function (e) {
            placeMarker(e.latLng, map);
        });
    }

    function placeMarker(position, map) {
        var marker = new google.maps.Marker({
            position: position,
            map: map
        });
        markersArray.push(marker);
        map.panTo(position);
    }

    google.maps.event.addDomListener(window, 'load', initialize);

    function deleteOverlays() {
        if (markersArray) {
            for (i in markersArray) {
                markersArray[i].setMap(null);
            }
            markersArray.length = 0;
        }
    }
   </script>
  </head>
  <body>
  <input type="button" value="deletemarker" onclick="deleteOverlays()" />
    <div id="map-canvas"></div>
  </body>
 </html>