2016-09-04 129 views
0

我想刪除Google地圖中的標記,但我不明白這一點。請幫幫我。刪除Google API V3中的標記

我Validation.js:

function initialize() { 
//geocodierungs Funktion damit Geocoding funktioniert 
geocoder = new google.maps.Geocoder(); 
var mapOptions = { 
    zoom:5, 
    center: new google.maps.LatLng(48.136607,11.577085), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 

var pos=new google.maps.LatLng(48.2,11); 
var marker = new google.maps.Marker({ 
    position:pos, 
    map:map, 
    title: 'test' 
});} setInterval(function(){   
     //$.post('nav_schnittstelle.php',{}).done(aktualisiereKartendaten); 
     alert('test'); 
     pos.setMap(null); },10000); 

我如何使用setMap(Null);?我不明白這一點。

回答

0

試着製作一個按鈕和一個偵聽器來測試你的代碼。

要從映射中移除標記,請調用傳遞null作爲參數的setMap()方法。

marker.setMap(null);

請注意,上述方法不會刪除標記。它只是從地圖上移除標記。如果您希望刪除標記,則應將其從地圖中刪除,然後將標記本身設置爲null

繼在document示例代碼:

// Adds a marker to the map and push to the array. 
function addMarker(location) { 
var marker = new google.maps.Marker({ 
position: location, 
map: map 
}); 
markers.push(marker); 
} 

// Sets the map on all markers in the array. 
function setMapOnAll(map) { 
for (var i = 0; i < markers.length; i++) { 
markers[i].setMap(map); 
} 
} 

// Removes the markers from the map, but keeps them in the array. 
function clearMarkers() { 
setMapOnAll(null); 
} 

// Shows any markers currently in the array. 
function showMarkers() { 
setMapOnAll(map); 
} 

// Deletes all markers in the array by removing references to them. 
function deleteMarkers() { 
clearMarkers(); 
markers = []; 
} 

要刪除一個標記,請參閱相關SO question代碼片段:

marker.addListener("dblclick", function() { 
marker.setMap(null); 
}); 

希望這有助於!