【问题标题】:Looking For a Correct Way to Hide or Display Streets on Map寻找在地图上隐藏或显示街道的正确方法
【发布时间】:2015-01-22 05:36:43
【问题描述】:

我正在尝试在This Demo 的 Google 地图上隐藏/显示道路图层,这对我有用,但您可以看到代码在添加样式时重复了很多次! 上:

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    map.set('styles', [{
        "featureType": "road",
            "stylers": [{
            "visibility": "off"
        }]
    }]);

    $('input:checkbox').change(function () {
        if ($(this).is(':checked')) {
            map.set('styles', [{
                "featureType": "road",
                    "stylers": [{
                    "visibility": "on"
                }]
            }]);
        } else {
            map.set('styles', [{
                "featureType": "road",
                    "stylers": [{
                    "visibility": "off"
                }]
            }]);
        }
    });

你能看一下演示,让我看看有什么更好的方法来实现这一点吗?无需多次重复样式?!

谢谢

【问题讨论】:

    标签: javascript jquery google-maps google-maps-api-3


    【解决方案1】:

    您可以将样式保存在变量中,例如:

    var stylesOff = [{
        "featureType": "road",
            "stylers": [{
            "visibility": "off"
        }]
    }];
    
    var stylesOn = [{
        "featureType": "road",
            "stylers": [{
            "visibility": "on"
        }]
    }];
    
    var myOptions = {
        zoom: 16,
        center: latlng,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: stylesOff
    };
    
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    $('input:checkbox').change(function () {
        if ($(this).is(':checked')) {
            map.setOptions({
                styles: stylesOn
            });
        } else {
            map.setOptions({
                styles: stylesOff
            });
        }
    });
    

    注意使用setOptions,这是更改地图选项的文档化方法。

    编辑:

    正如您在评论中所说,您想独立切换多种样式,这是一个解决方案。

    它使用输入 data 属性来识别复选框所指的样式。

    <input type="checkbox" class="styles-control" data-style="road" checked="checked"> Roads
    <input type="checkbox" class="styles-control" data-style="poi" checked="checked"> POI
    <input type="checkbox" class="styles-control" data-style="administrative.locality" checked="checked"> Localities
    <input type="checkbox" class="styles-control" data-style="water" checked="checked"> Water
    

    基于此,您可以在其中一个复选框更改时构建样式数组:

    // Get all styles controls
    var controls = $('.styles-control');
    
    $('.styles-control').change(function () {
    
        styles = [];
    
        // Loop through styles controls
        for (var i = 0; i < controls.length; i++) {
    
            if ($(controls[i]).is(':checked')) {
    
                // Push the feature type to the styles array
                // The feature type is based on the input data-style attribute
                styles.push({
                    "featureType": $(controls[i]).data('style'),
                        "stylers": [{
                        "visibility": "on"
                    }]
                });
            } else {
                styles.push({
                    "featureType": $(controls[i]).data('style'),
                        "stylers": [{
                        "visibility": "off"
                    }]
                });
            }
        }
    
        map.setOptions({
    
            styles: styles
        });
    });
    

    JSFiddle demo

    【讨论】:

    • 嗨 MrUpsidown,感谢您的回复,我喜欢您这样做的方式,但我只是注意到这里有一个问题,我以前没有提到过(对不起!我的坏!)。关键是如果我们只有一个用于打开或关闭道路的复选框,则此解决方案可以完美运行,但如果我们有多个选项,例如街道、行政甚至水,该怎么办。如果一个没有被选中或一些被选中,如何打开或关闭所有样式?谢谢
    • 非常感谢,这正是我想要的
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多