Google maps api 3 隐藏多段线的正确方法?

Google maps api 3 隐藏多段线的正确方法?,google-maps-api-3,Google Maps Api 3,我有一个在地图上显示多段线的函数,这部分正在工作,现在我想实现一个隐藏多段线的函数,但是我找不到我的错误,提前谢谢 function cargaMapaCYL(mapa, varControl){ var limite = null; limite = [ new google.maps.LatLng(42.49956716,-7.019005501), new google.maps.LatLng(42.49947126,-7.02928637

我有一个在地图上显示多段线的函数,这部分正在工作,现在我想实现一个隐藏多段线的函数,但是我找不到我的错误,提前谢谢

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

只需创建一次多段线。将其放入全局变量contorno=。。。然后可以使用setVisible(布尔)方法创建切换函数

Blow是一个在3秒后隐藏路径的示例

/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}

//此示例创建一条2像素宽的红色多段线,显示William的路径
//Kingsford Smith在加利福尼亚州奥克兰和加利福尼亚州之间的首次跨太平洋航班
//澳大利亚布里斯班。
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:{lat:0,lng:-180},
mapTypeId:'地形'
});
var FlightPlan坐标=[
{lat:37.772,lng:-122.214},
{lat:21.291,lng:-157.821},
{拉丁美洲:-18.142,液化天然气:178.431},
{拉丁美洲:-27.467,液化天然气:153.027}
];
var flightPath=new google.maps.Polyline({
路径:FlightPlan坐标,
测地线:正确,
strokeColor:“#FF0000”,
笔划不透明度:1.0,
冲程重量:2
});
flightPath.setMap(map);
setTimeout(函数(){
警报(“隐藏路径”);
flightPath.setVisible(false);
}, 3000);
}

每次调用函数时,它都会创建一条新的多段线。无论是否添加到地图中

很可能,您希望能够使用true调用函数一次以添加行,然后再次使用false将其删除。此时,当您再次调用它时,它会创建一条新线,而不会将其添加到地图中。它不触及已添加到地图中的原始线

一种方法是将行保留在全局变量中。然后可以在调用之间引用完全相同的对象

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