删除 Google API V3 中的标记

dea*_*gon 5 javascript maps google-maps-api-3 marker

我想删除 Google 地图中的标记,但我不\xc2\xb4t 理解这一点。请帮我。

\n\n

我的 Validation.js:

\n\n
function initialize() {\n//geocodierungs Funktion damit Geocoding funktioniert\ngeocoder = new google.maps.Geocoder();  \nvar mapOptions = {\n    zoom:5,\n    center: new google.maps.LatLng(48.136607,11.577085),\n    mapTypeId: google.maps.MapTypeId.ROADMAP\n};\nmap = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);\n\nvar pos=new google.maps.LatLng(48.2,11);\nvar marker = new  google.maps.Marker({\n    position:pos,\n    map:map,\n    title: 'test'\n});}    setInterval(function(){         \n       //$.post('nav_schnittstelle.php',{}).done(aktualisiereKartendaten);\n       alert('test');\n       pos.setMap(null); },10000);\n
Run Code Online (Sandbox Code Playgroud)\n\n

我该如何使用setMap(Null);?我不明白这一点。

\n

Mr.*_*bot 7

尝试制作一个按钮和一个侦听器来测试您的代码。

要从地图中删除标记,请调用 setMap() 方法并传递 null 作为参数。

标记.setMap(null);

请注意,上述方法不会删除标记。它只是从地图上删除标记。如果您希望删除标记,则应将其从地图中删除,然后将标记本身设置为null

按照文档中的示例代码:

// 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 = [];
}
Run Code Online (Sandbox Code Playgroud)

要删除单个标记,请参阅相关的SO 问题代码片段:

marker.addListener("dblclick", function() {
marker.setMap(null);
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!