【问题标题】:Remove Marker in Google API V3删除 Google API V3 中的标记
【发布时间】:2017-01-12 03:30:38
【问题描述】:

我想删除 Google 地图中的标记,但我不明白这一点。请帮帮我。

我的 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);?我不明白这一点。

【问题讨论】:

    标签: javascript google-maps-api-3 maps marker


    【解决方案1】:

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

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

    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 代码 sn-p:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 2011-12-19
      • 2012-07-20
      • 2012-05-24
      • 2013-05-02
      • 2015-07-16
      • 1970-01-01
      • 2011-02-25
      • 2012-01-24
      相关资源
      最近更新 更多