Toggle KML Layers in Google Maps v3

前端 未结 2 2144
盖世英雄少女心
盖世英雄少女心 2020-12-29 16:36

Is there a simple way to set up checkboxes to toggle (on/off) KML layers for Google Maps v3? I\'ve come across these articles, but none of them have worked for me.

2条回答
  •  太阳男子
    2020-12-29 16:59

    For toggling kml layer you can try this function:

    /** Toggles layer
    * @param {google.maps.KmlLayer} layer
    * @param {google.maps.Map} map
    **/ 
    function toggleLayer( layer, map ){
         layer.setMap( layer.getMap() ? null : map );
    }
    

    Store your layers in array for farther actions (e.g. toggling).

    Then you can toggle layer by it's index on that array:

    toggleLayer( layersArray[index], map );
    

    Or to apply toggling to all layers:

    for( var index = 0; index < layersArray.length; ++index){
        toggleLayer( layersArray[index], map );
    }
    

提交回复
热议问题