谷歌地图api清除标记

时间:2012-12-24 10:29:39

标签: javascript google-maps-api-3

嗨,我在删除地图上的标记时遇到问题。我做了一个事件,如果我的下拉列表会改变标记将被删除。到目前为止,它不起作用。我会做错的任何人都会感激。这是我的代码。删除叠加层的功能是filtermark()。对不起我是javascript的菜鸟。

        function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(10.4167, 123.0000),
    zoom: 8,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;


  downloadUrl("xmlspitter.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });


function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

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


 }

1 个答案:

答案 0 :(得分:-1)

修正了问题是我没有声明var mapsvar markers也将所有标记推送到markersArray.push(marker);的数组中。