2017-12-02 83 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