只允许在Google地图中使用一个标记

时间:2017-12-02 22:55:39

标签: javascript google-maps

我知道存在关于这个主题的一些线索,但我相信这有点不同,因为,所有线程都是关于一个标记点​​击地图,但这是每次唯一的标记。看看下面的代码

...
var x = map.setCenter(new google.maps.LatLng(lat, lng), 17);
        marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        center: new google.maps.LatLng(lat, lng),
        map: map,
        title: 'Map Tracker',
});
...

在每30秒内,使用不同的[lat]和[lng]值调用此代码,但所有旧标记仍然可见,并且我不知道如何删除它们,我希望每个结果只有一个标记。此外,我已经尝试过map.clearOverlays()但仍然无法正常工作。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

Google Maps API有一个API调用,用于设置visible与否,在将x重新绑定到新地图之前,您可以将可见性设置为false。

...
if (x !== undefined && x !== null) { x.setVisible(false) }

var x = map.setCenter(new google.maps.LatLng(lat, lng), 17);
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    center: new google.maps.LatLng(lat, lng),
    map: map,
    title: 'Map Tracker',
});
...