-3
我在移除Google地圖上的標記時遇到問題。這些標記是每次用戶創建航點時創建的。他們的位置與航點相同。這些標記也有一個信息框,帶有一個按鈕來刪除正確的航點和標記。Google地圖無法移除標記
每次創建一個航點時,我都會將一個標記推送到一個數組,並將一個信息框推送到另一個。
這有效,如果我只有一個航點。標記和航點被刪除。但一旦我有2個或更多,它不再工作。一旦我嘗試刪除標記停留在地圖上的某個路標,即使我調用了.setMap(null)和/或.setPosition(null)。每次我刪除一個標記我也刪除它,從他們相應的陣列的航點和信息框。奇怪的是,當我調用一個重繪路徑的函數時,航點不再存在(按計劃),但標記保持不變。
下面是代碼:
var markers = {
waypoints:{
p:[],
i:[],
m:[]
}
};
var saveWaypoints = function(){
removeWaypoints();
var points = directionsRenderer.getDirections().routes[0].legs[0].via_waypoints;
for(var i=0;i<points.length;i++){
var marker = new google.maps.Marker({
map:map,
position:points[i],
icon: 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png',
id: i
});
var infobox = new InfoBox({
id: i,
position: points[i],
disableAutoPan: true,
boxStyle: {
background: "white",
opacity: 1,
width: "289px",
border: 0,
padding: 0
},
zIndex: 1E4,
isHidden: false,
pane: "floatPane",
enableEventPropagation: false,
content: 'Pozicija: ' + points[i]
});
infobox.open(map,marker);
infobox.hide();
google.maps.event.addListener(infobox,'closeclick',function(){
markers.waypoints.m[this.id_].setMap(null);
markers.waypoints.m[this.id_].setPosition(null);
markers.waypoints.m.splice(this.id_,1);
markers.waypoints.p.splice(this.id_,1);
markers.waypoints.i.splice(this.id_,1);
drawPath();
});
google.maps.event.addListener(marker,'mouseover',function(){
markers.waypoints.i[this.id].show();
});
google.maps.event.addListener(marker,'mouseout',function(){
if(markers.waypoints.i[this.id] !== undefined){
if(!markers.waypoints.i[this.id].keepOpen()){
markers.waypoints.i[this.id].hide();
}
}
});
google.maps.event.addListener(marker,'click',function(){
if(waypointDialog !== null){
waypointDialog.doNotClose(false);
waypointDialog.hide();
}
console.log(this.id);
waypointDialog = markers.waypoints.i[this.id];
waypointDialog.doNotClose(true);
});
markers.waypoints.m.push(marker);
markers.waypoints.i.push(infobox);
markers.waypoints.p.push({
location: points[i],
stopover: false
});
};
};
var removeWaypoints = function(){
if(markers.waypoints !== (null||undefined)){
markers.waypoints.p = [];
markers.waypoints.m = [];
markers.waypoints.i = [];
}
};
清除磁盤陣列不會從地圖中刪除標記,必須遍歷'markers.waypoints.m'並設置每個標記爲'null' –
我做的地圖屬性,但不幸的是,這並沒有解決問題。 – user1824933
要從地圖上移除標記,您需要將地圖屬性設置爲空。使用google.maps.Marker中的setMap方法來設置它 – cadetill