2013-01-17 145 views
0

我已搜索並搜索了一個沒有運氣的答案。我可以得到XML數據顯示正常,它似乎是我的標記刷新,但不會刪除前一個,所以他們堆疊起來。任何幫助將是偉大的!我只需要在出現新標記之前刪除標記。謝謝!谷歌地圖API V3更新前清除標記

<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> 

回答

2

的問題是,您不添加您的標記,以markerArray,它保持空的,所以沒有什麼,當你調用resetMarkers

創建標記後取出,將其添加到markerArray:

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