JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – Google Maps Polyline – 如何删除?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我检查了以前的问题,这些都与V2有关,这是没有帮助的.

所以,我创建两个标记,将它们保存在数组中作为标记[“to”]和标记[“from”]

然后再添加它们

function route(){
        for(var key in markers) {
           flightPlanCoordinates.push(markers[key].position);
       }
        flightPath = new google.maps.Polyline({
            path: flightPlanCoordinates,strokeColor: "#FF0000",strokeOpacity: 1.0,strokeWeight: 2
    });
   flightPath.setMap(map);
}

辉煌.
但.下次我使用它(使用数组中的新标记),它只是添加一个折线,而不删除之前的一个.
我似乎尝试了所有的东西,从第一个阵列,flightPath,setMap(null)等删除.

在绘制新行前,删除上一行的正确方法是什么?

编辑:解决

function route(){
    var flightPlanCoordinates = [];
    for(var key in markers) {
        flightPlanCoordinates.push(markers[key].position);
    }
    if(flightPath) {
        flightPath.setPath(flightPlanCoordinates);
    } else {
        flightPath = new google.maps.Polyline({
            path: flightPlanCoordinates,strokeWeight: 2
        });
        flightPath.setMap(map);
    }
}

原因:
flightPlanCoordinates需要在范围内进行初始化,这将在每次使用时重置数组,将其正确清理. (还要感谢下面的输入,使代码更好一点.

解决方法

在flightPath = new …之前,我看不到var,所以我认为flightPath是一个全局变量.
function route(){
   //flightPath.setMap(null); Doesnt't work!?
   for(var key in markers) {
       flightPlanCoordinates.push(markers[key].position);
   }
   if(flightPath) {//If flightPath is already defined (already a polylinE)
       flightPath.setPath(flightPlanCoordinates);
   } else {
       flightPath = new google.maps.Polyline({
           path: flightPlanCoordinates,strokeWeight: 2
      });
      flightPath.setMap(map);//It's not necessary to setMap every time
   }

}

大佬总结

以上是大佬教程为你收集整理的javascript – Google Maps Polyline – 如何删除?全部内容,希望文章能够帮你解决javascript – Google Maps Polyline – 如何删除?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。