[英]Can't remove MVCArray/Polylines using Google Maps API V3
我有一個MVCArray來存儲我的折線路徑,但是當用戶更改其選擇時,我需要清除MVCArray。 相關代碼如下。
routePoints = new google.maps.MVCArray();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
markersArray = eval("(" + xmlhttp.responseText + ")");
removeLines();
for (var i = 0; i < markersArray.length; i++) {
var address = new google.maps.LatLng(markersArray[i][0], markersArray[i][1]);
routePoints.insertAt(routePoints.length, address);
}
routePath = new google.maps.Polyline({
path: routePoints,
strokeOpacity: 1.0,
strokeWeight: 2,
map: map,
});
}
}
removeLines()函數如下。 我嘗試了此函數的許多不同版本(while循環,routePoints.pop(),將routePoints.length設置為0),但是沒有任何東西可以清除MVCArray。 折線可以正確顯示,但是一旦用戶更改了選擇,我就需要從地圖上刪除以前的折線。 提前致謝。
function removeLines() {
if (routePoints) {
for (var i=0; i < routePoints.length; i++) {
routePoints.removeAt(i);
}
}
}
因此routePoints只是LatLng對象的數組,而不是單獨的折線。 我認為您可能需要一個用於折線的單獨數組,然后可以循環將其刪除。
如果要刪除可見的折線,則可以調用setMap()函數,並將其傳遞為null
。
routePoints = new google.maps.MVCArray();
var polylines = []; // new array for the polylines, needs to be a global variable
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
markersArray = eval("(" + xmlhttp.responseText + ")");
removeLines();
for (var i = 0; i < markersArray.length; i++) {
var address = new google.maps.LatLng(markersArray[i][0], markersArray[i][1]);
routePoints.insertAt(routePoints.length, address);
}
routePath = new google.maps.Polyline({
path: routePoints,
strokeOpacity: 1.0,
strokeWeight: 2,
map: map,
});
// add the polyline to the array
polylines.push(routePath);
}
}
function removeLines() {
for (var i=0; i < polylines.length; i++) {
polylines[i].setMap(null);
}
// you probably then want to empty out your array as well
polylines = [];
// not sure you'll require this at this point, but if you want to also clear out your array of coordinates...
routePoints.clear();
}
刪除所有mvcarray元素:
routePoints.clear();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.