Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t使用Google Maps API V3删除MVCArray/多段线_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript Can';t使用Google Maps API V3删除MVCArray/多段线

Javascript Can';t使用Google Maps API V3删除MVCArray/多段线,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个MVCArray来存储多段线路径,但当用户更改其选择时,我需要清除MVCArray。相关代码如下 routePoints = new google.maps.MVCArray(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { markersArray = eval("(" + x

我有一个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,
                });
            }
        }
routePoints=new google.maps.MVCArray();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
markersArray=eval(“+xmlhttp.responseText+”);
removeLines();
对于(var i=0;i
下面是removeLines()函数。我尝试过此函数的许多不同版本(while loop,routePoints.pop(),将routePoints.length设置为0),但没有任何东西清除MVCArray。顺便说一下,多段线显示正确,但一旦用户更改其选择,我需要从地图中删除以前的多段线。提前谢谢

function removeLines() {
        if (routePoints) {
            for (var i=0; i < routePoints.length; i++) {
                routePoints.removeAt(i);
            }
        }
    }
函数removeLines(){
if(路由点){
对于(变量i=0;i
因此routePoints只是一个板条对象数组,而不是单个多段线。我认为您可能需要为多段线创建一个单独的数组,然后您可以循环将其全部删除。
如果要删除可见多段线,只需调用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();
}
routePoints=new google.maps.MVCArray();
变量多段线=[];//多段线的新数组需要是全局变量
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
markersArray=eval(“+xmlhttp.responseText+”);
removeLines();
对于(var i=0;i
因此routePoints只是一个板条对象数组,而不是单个多段线。我认为您可能需要为多段线创建一个单独的数组,然后您可以循环将其全部删除。
如果要删除可见多段线,只需调用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();
}
routePoints=new google.maps.MVCArray();
变量多段线=[];//多段线的新数组需要是全局变量
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
markersArray=eval(“+xmlhttp.responseText+”);
removeLines();
对于(var i=0;i
要删除所有mvcarray元素:

routePoints.clear();

要删除所有mvcarray元素,请执行以下操作:

routePoints.clear();

谢谢你的意见,但这不起作用。Firebug显示“polylines.setMap不是函数”的错误。当我隐藏polylines.setMap(null)行时,Firebug中没有任何错误消息,映射返回工作状态,但新用户选择后仍没有删除多段线。感谢您的输入,但这不起作用。Firebug显示“polylines.setMap不是函数”的错误。当我隐藏polylines.setMap(null)行时,Firebug中没有收到任何错误消息,映射将恢复工作,但新用户选择后仍不会删除多段线。