2012-12-24 223 views
-1

嗨我有一個問題,刪除我的地圖上的標記。我做了一個事件,如果我的下拉列表會改變標記將被刪除。到目前爲止它不工作。如果我做錯了,會感謝任何人。這是我的代碼。將刪除覆蓋的功能是filtermark()。對不起,我是JavaScript的noob。谷歌地圖API清除標記

 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

固定它的問題是我沒有申報var mapsvar markers也推動所有標記成陣列markersArray.push(marker);