Man*_*tar 9 javascript google-maps
所以我检查了以前关于这个的问题,这些问题都与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);
}
Run Code Online (Sandbox Code Playgroud)
辉煌.但.下次我使用它(在数组中使用新标记)它只是在那里添加折线而不删除前一个.我似乎尝试了一切,从第一个数组中删除了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);
}
}
Run Code Online (Sandbox Code Playgroud)
原因:flightPlanCoordinates需要在范围内初始化,这会在每次使用时重置数组,并正确清理它.(还要感谢下面的输入,使代码更好一些.
假设"mypolyline"是您的Polyline对象,您还可以尝试:
mypolyline.setPath([]);
Run Code Online (Sandbox Code Playgroud)
这样,您将从Polyline中取出坐标,这实际上将从地图中删除它.
var我之前没有看到flightPath = new...,所以我认为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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21381 次 |
| 最近记录: |