0

我想根据用户点击位置类型查看和删除地图上的标记。

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

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);
                } 
            }); 
4

1 回答 1

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>
于 2013-05-08T09:03:59.547 回答