本文介绍了Google地图折线 - 如何删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了两个标记,将它们保存在一个数组中作为标记[to]和标记[from]



然后将它们与这添加



<$ p (标记[key] .position); $ p $ 函数route(){
(标记中的var键){
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,
strokeColor:#FF0000,
strokeOpacity:1.0,
strokeWeight:2
});
flightPath.setMap(map);






原因:
flightPlanCoordinates需要初始化在范围内,每次使用时都会重置阵列,并将其正确清理。 (也感谢下面的输入,使代码更好一些。

解决方案

我没有看到 var 之前 flightPath = new ... ,所以我认为 flightPath 是一个全局变量。

 函数route(){
//flightPath.setMap(null); does not't work!?
for(var key in markers){
flightPlanCoordinates.push(markers [key] .position);
}
if(flightPath){//如果已经定义了flightPath(已经是多段线)
flightPath.setPath(flightPlanCoordinates);
} else {
flightPath = new google.maps.Polyline({
path:flightPlanCoordinates,
strokeColor: #FF0000,
strokeOpacity:1.0,
strokeWeight:2
});
flightPath.setMap(map); //不需要每次设置map
}

}


So I checked previous questions regarding this, which all relate to V2, which is of no help.

So, I create two markers, save them in an array as markers["to"] and markers["from"]

And then add them with this

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);
}

Brilliant.But. Next time I use it (With new markers in the array) it just adds a polyline there without removing the previous one.I seem to have tried everything, removing from the first array, the flightPath, setMap(null) and so forth.

What's the correct way to remove the previous line before drawing a new one?

EDIT: SOLVEDSolution

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,
            strokeColor: "#FF0000",
            strokeOpacity: 1.0,
            strokeWeight: 2
        });
        flightPath.setMap(map);
    }
}

Reason:flightPlanCoordinates needs to be initialized within the scope, this resets the array every time it is used, cleaning it out properly. (Also thanks for the input below to make the code a bit nicer.

解决方案

I don't see var before flightPath = new..., so I presume flightPath is a global variable.

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,
           strokeColor: "#FF0000",
           strokeOpacity: 1.0,
           strokeWeight: 2
      });
      flightPath.setMap(map);//It's not necessary to setMap every time
   }

}

这篇关于Google地图折线 - 如何删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:13