2017-12-02 86 views
-1

我知道存在一些关于此主题的线索,但我认为这有点不同,因为所有线索都是关于点击地图上的一个标记,但这是唯一标记每一次。看看下面只允许在Google地图中使用一个标记

... 
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秒这段代码,这段代码被称为具有不同的[纬度]和[经度]值,但所有旧标志仍清晰可见,我不知道如何删除它们,我每个结果只需要一个标记。此外,我已经尝试map.clearOverlays(),但仍然无法工作。

你能帮我吗?

+0

答案与其他问题相同。从地图上移除标记,然后用新标记覆盖标记或将其移至新位置。 – geocodezip

+0

我已经试过了!仍然没有工作,没有错误:) – Clarice

+0

洪水持续:http://prntscr.com/hib0s6 – Clarice

回答

0

在将x重新绑定到新地图之前,Google Maps API有一个用于设置visible的API调用,您可以将可见性设置为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', 
}); 
... 
+0

仍然无法正常工作...洪水持续存在:http://prntscr.com/hib0s6 – Clarice

+1

嗯,我们可以看到更多的机会码?通常下面的东西应该工作,我认为它是在你正在取代现有的标记变量。 –

+0

不知道你为什么要检查'x'并尝试调用'setVisible','map.setCenter'不会返回任何东西,根据[文档](https://developers.google.com/maps/documentation /javascript/3.exp/reference#Map),它的返回值是:'返回值:无' – geocodezip