javascript 在 Google Maps v3 中切换 KML 图层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9855067/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:03:12  来源:igfitidea点击:

Toggle KML Layers in Google Maps v3

javascriptgoogle-maps-api-3kml

提问by kaoscify

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

是否有一种简单的方法可以设置复选框以切换(打开/关闭)Google Maps v3 的 KML 图层?我遇到过这些文章,但没有一篇对我有用。

回答by efwjames

Sorry my article didn't work for you, it is a little dated.

抱歉,我的文章对您不起作用,它有点过时了。

Here is a full example of a typical Gmaps toggle assuming you are using kml files.

这是一个典型的 Gmaps 切换的完整示例,假设您使用的是 kml 文件。

<html>
<head>

 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">

var map;

// lets define some vars to make things easier later
var kml = {
    a: {
        name: "MPLS/STPL",
        url: "https://maps.google.com/maps/ms?authuser=0&vps=5&ie=UTF8&msa=0&output=kml&msid=212971981154994583939.0004b06640255267e038c"
    },
    b: {
        name: "Bicycling Tour Routes",
        url: "https://maps.google.com/maps/ms?authuser=0&vps=4&ie=UTF8&msa=0&output=kml&msid=212971981154994583939.0004902a1824bbc8c0fe9"
    }
// keep adding more if ye like 
};

// initialize our goo
function initializeMap() {
    var options = {
        center: new google.maps.LatLng(44.9812, -93.2687),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), options);

    createTogglers();
};

google.maps.event.addDomListener(window, 'load', initializeMap);

// the important function... kml[id].xxxxx refers back to the top 
function toggleKML(checked, id) {

    if (checked) {

        var layer = new google.maps.KmlLayer(kml[id].url, {
            preserveViewport: true,
            suppressInfoWindows: true 
        });
        // store kml as obj
        kml[id].obj = layer;
        kml[id].obj.setMap(map);
    }
    else {
        kml[id].obj.setMap(null);
        delete kml[id].obj;
    }

};

// create the controls dynamically because it's easier, really
function createTogglers() {

    var html = "<form><ul>";
    for (var prop in kml) {
        html += "<li id=\"selector-" + prop + "\"><input type='checkbox' id='" + prop + "'" +
        " onclick='highlight(this,\"selector-" + prop + "\"); toggleKML(this.checked, this.id)' \/>" +
        kml[prop].name + "<\/li>";
    }
    html += "<li class='control'><a href='#' onclick='removeAll();return false;'>" +
    "Remove all layers<\/a><\/li>" + 
    "<\/ul><\/form>";

    document.getElementById("toggle_box").innerHTML = html;
};

// easy way to remove all objects
function removeAll() {
    for (var prop in kml) {
        if (kml[prop].obj) {
            kml[prop].obj.setMap(null);
            delete kml[prop].obj;
        }

    }
};


// Append Class on Select
function highlight(box, listitem) {
    var selected = 'selected';
    var normal = 'normal';
    document.getElementById(listitem).className = (box.checked ? selected: normal);
};

function startup() { 
// for example, this toggles kml b on load and updates the menu selector
var checkit = document.getElementById('b');
checkit.checked = true;
toggleKML(checkit, 'b');
highlight(checkit, 'selector1');
 }

</script>

<style type="text/css">
.selected { font-weight: bold; }
</style>

</head>
<body onload="startup()">
<div id="map_canvas" style="width: 100%; height: 600px;"></div>
<div id="toggle_box" style="position: absolute; top: 100px; right: 20px; padding: 10px; background: #fff; z-index: 5; "></div>
</body>
</html>

This is pure js so of course using jQuery you can do things a little easier. Hope this helps!

这是纯 js 所以当然使用 jQuery 你可以做的事情更容易一些。希望这可以帮助!

回答by Engineer

For toggling kml layer you can try this function:

要切换 kml 层,您可以尝试使用此功能:

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