Javascript Google 地图折线 - 如何删除它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps Polyline - How do I remove it?
提问by Mantar
So I checked previous questions regarding this, which all relate to V2, which is of no help.
所以我检查了之前关于这个的问题,这些问题都与 V2 相关,这没有帮助。
So, I create two markers, save them in an array as markers["to"] and markers["from"]
所以,我创建了两个标记,将它们保存在一个数组中作为标记 ["to"] 和标记 ["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.
杰出的。但。下次我使用它时(在数组中使用新标记)它只是在那里添加了一条折线而不删除前一条。我似乎已经尝试了一切,从第一个数组中删除,flightPath,setMap(null) 等等。
What's the correct way to remove the previous line before drawing a new one?
在绘制新线之前删除前一行的正确方法是什么?
EDIT: SOLVED Solution
编辑:解决方案
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.
原因:flightPlanCoordinates 需要在范围内初始化,这会在每次使用时重置数组,正确清理它。(也感谢下面的输入使代码更好一些。
采纳答案by Kerstomaat
I don't see var
before flightPath = new...
, so I presume flightPath
is a global variable.
我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
}
}
回答by DemitryT
Assuming that "mypolyline" is your Polyline object, you can also try:
假设“mypolyline”是您的 Polyline 对象,您还可以尝试:
mypolyline.setPath([]);
This way, you are taking out the coordinates from the Polyline, which in effect will remove it from the map.
这样,您将从折线中取出坐标,这实际上会将其从地图中删除。
回答by nguyên
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;
}
回答by Aravind Asok
flightPath is just an array of LatLng objects, not individual polylines. I think you probably need a separate array for the polylines, which you can then loop over to remove them all. Create a global array line.
flightPath 只是一个 LatLng 对象数组,而不是单个折线。我认为您可能需要一个单独的多段线数组,然后您可以循环删除它们。创建一个全局数组行。
var line = [];
flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
line.push(flightPath);
Now you are pushing all the polyline objects into an array line. You can make it invisible or remove it from the map by looping it like this:
现在您将所有折线对象推入一个阵列线。您可以通过如下循环使其不可见或从地图中删除它:
for (i=0; i<line.length; i++)
{
line[i].setMap(null); //or line[i].setVisible(false);
}
回答by Rajkumar
set strokeWeight: 0 then polyline will hide
设置 strokeWeight: 0 然后折线将隐藏