正确的方法来隐藏折线? [英] The correct way to hide a polyline?
问题描述
我是一个在地图上显示多段线的函数,这部分工作正常,现在我想实现一个隐藏多段线的函数,但是我无法预先找到我的错误。
函数cargaMapaCYL(mapa,varControl){
var limite = null;
limite = [
new google.maps.LatLng(42.49956716,-7.019005501),
new google.maps.LatLng(42.49947126,-7.029286373),
new google.maps。 LatLng(42.50904062,-7.049299123),
新google.maps.LatLng(42.50722622,-7.069103626),
新google.maps.LatLng(42.50452387,-7.000150672),
新google.maps .LatLng(42.49348015,-6.983058917),
新google.maps.LatLng(42.49843269,-6.971666546),
新google.maps.LatLng(42.51765791,-6.956909023),
新谷歌。 maps.LatLng(42.52010069,-6.927429186),
新google.maps.LatLng(42.50992238,-6.914231493),
新google.maps.LatLng(42.50096695,-6.879679821),
新google .maps.LatLng(42.48775868,-6.857775832),
new google.maps.LatLng(43.23907504,-3.293216584)],#000000,5);
var contorno = new google.maps.Polyline({
path:limite,
strokeColor:#000000,
strokeOpacity:1.0,
strokeWeight:2
});
if(varControl == true){
contorno.setMap(mapa);
}
if(varControl == false){
contorno.setMap(null);
}
}
你的函数被调用,它创建一个新的折线。然后它或者添加到地图中。
可能你希望能够用true调用该函数来添加该行,然后再用false来删除它。目前,当你第二次调用它时,它会创建一个新行并且不会将其添加到地图中。它不会触及已添加到地图的原始线条。
一种方法是将线保留在全局变量中。然后可以参考调用之间完全相同的对象。
var contorno = null;
函数cargaMapaCYL(mapa,varControl){
if(!contorno){
var limite = [...],#000000,5);
contorno = new google.maps.Polyline({...});
}
if(varControl){
contorno.setMap(mapa);
} else {
contorno.setMap(null);
}
}
I´ve a function that show a polyline on a map, this part is working, now I want to implement a function that hides the polyline, but I can´t find my mistake, thanks in advance.
function cargaMapaCYL(mapa, varControl){
var limite = null;
limite = [
new google.maps.LatLng(42.49956716,-7.019005501),
new google.maps.LatLng(42.49947126,-7.029286373),
new google.maps.LatLng(42.50904062,-7.049299123),
new google.maps.LatLng(42.50722622,-7.069103626),
new google.maps.LatLng(42.50452387,-7.000150672),
new google.maps.LatLng(42.49348015,-6.983058917),
new google.maps.LatLng(42.49843269,-6.971666546),
new google.maps.LatLng(42.51765791,-6.956909023),
new google.maps.LatLng(42.52010069,-6.927429186),
new google.maps.LatLng(42.50992238,-6.914231493),
new google.maps.LatLng(42.50096695,-6.879679821),
new google.maps.LatLng(42.48775868,-6.857775832),
new google.maps.LatLng(43.23907504,-3.293216584)], "#000000", 5);
var contorno= new google.maps.Polyline({
path: limite,
strokeColor: "#000000",
strokeOpacity: 1.0,
strokeWeight: 2
});
if(varControl==true){
contorno.setMap(mapa);
}
if(varControl==false){
contorno.setMap(null);
}
}
Every your function is called, it creates a new polyline. Which it either then adds to the map or not.
Persumably you want to be able to call the function once with true to add the line, then again with false to remove it. At the moment, when you call it a second time, its creates a new line and doesnt add it to the map. It does not touch the original line already added to the map.
One way is too keep the line in a global variable. Then can refer to the exact same object between calls.
var contorno = null;
function cargaMapaCYL(mapa, varControl){
if (!contorno) {
var limite = [...], "#000000", 5);
contorno= new google.maps.Polyline({...});
}
if(varControl){
contorno.setMap(mapa);
} else {
contorno.setMap(null);
}
}
这篇关于正确的方法来隐藏折线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!