所以我检查了之前关于这方面的问题,这些问题都与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,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
}
原因: flightPlanCoordinates需要在作用域内初始化,这会在每次使用时重置数组,并将其正确清除。(也感谢您在下面的输入,使代码更好一些。
发布于 2010-12-30 19:10:35
我没有在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,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);//It's not necessary to setMap every time
}
}
发布于 2011-10-17 18:13:30
假设"mypolyline“是您的Polyline对象,您也可以尝试:
mypolyline.setPath([]);
这样,您将从多段线中取出坐标,这实际上会将其从地图中删除。
发布于 2011-03-25 08:07:44
function traffic(map){
// polyline
this.path=null;
this.map = google.maps.Map(ele, opt);
}
traffic.prototype._draw = function()
{
//create new polyline
var path = new google.maps.Polyline({
path: this.get('latlngArr'),
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
//delete old
var prepath = this.path;
if(prepath){
prepath.setMap(null);
}
//new polyline
path.setMap(this.map);
this.path = path;
}
https://stackoverflow.com/questions/4565260
复制