Google Maps API V3在更新前清除标记

时间:2013-01-17 22:21:12

标签: javascript google-maps

我搜索并搜索了一个没有运气的答案。我可以让xml数据显示正常,看起来我的标记刷新但只是不会删除前一个,所以他们堆叠起来。任何帮助都会很棒!我只需要在新标记出现之前删除标记。谢谢!

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
    //<![CDATA[
var markersArray = [];
    var customIcons = {
      restaurant: {
        icon: 'pin.png',
        shadow: ''
      },
      bar: {
        icon: 'loc1.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
airport: {
        icon: 'loc2.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

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

      // Change this depending on the name of your PHP file
setInterval(function() {

      downloadUrl("phpsqlajax_genxmlall.php", function(data) {

        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
resetMarkers(markersArray)
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
var amount = markers[i].getAttribute("amount");
var time = markers[i].getAttribute("time");
          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 + "<br/>$" + amount + " at " + time;
          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);

        }


      });
}, 5000);
    }
function resetMarkers(arr){
    for (var i=0;i<arr.length; i++){
        arr[i].setMap(null);
    }
    //reset the main marker array for the next call
    arr=[];
}
    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() {}

    //]]>

  </script>

  </head>

  <body onload="load()">

    <div id="map" style="width: 1400px; height: 800px"></div>
  </body>

1 个答案:

答案 0 :(得分:2)

问题是,您没有将标记添加到markerArray,它会保持为空,因此在您拨打resetMarkers时无需删除

创建标记后,将其添加到markerArray:

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