[英]Google Maps/Remove markers in backbone.js view
我是JS和bone.js的新用戶。
我有一個主干應用程序,正在向Google Maps api發出請求,並根據該應用程序中的搜索成功刪除了圖釘。
現在,我想在搜索新標記時從地圖上刪除舊標記。
這是我的addMarker函數:
function addmarker(model) {
var self = this,
marker;
_geocode.call(this, model.get('address'))
.done(function(location) {
marker = new google.maps.Marker({
map: self.map,
position: location
});
_infowindow.call(self, marker, model);
})
.fail(function() {
console.log("This address cannot be retrieved from the server");
});
};
我想在此函數調用開始時刪除舊標記。 我嘗試將標記存儲在數組中,並根據API文檔使用clearMarkers函數在每個元素上調用setMap(null)函數。
https://developers.google.com/maps/documentation/javascript/examples/marker-remove
這沒有用。
在這種情況下,刪除標記的最有效方法是什么?
如果您對我的其余代碼有疑問,請告訴我。
您需要創建一個全局標記數組,並將每個標記推入其中。 例如:
var markers = [];
然后在addMarker()
函數中:
markers.push(marker); // where marker is your google.Maps.Marker();
因此,如果要刪除所有標記:
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null);
}
將其包裝在函數中和/或首先在addMarker()
函數中調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.