如何显示ol-3层切换器

时间:2018-05-31 13:01:41

标签: php google-maps gis mapbox openlayers-3

我正在开发GIS,但我无法显示Layer Switcher。在这种情况下,我使用了Open Layers 3.

map = new ol.Map({
        controls: ol.control.defaults({
          attribution: false
        }).extend([mousePositionControl,zoomslider]),
        target: document.getElementById('map'),
        layers: [
    new ol.layer.Group({
        'title': 'BaseMaps',
        layers: [
        new ol.layer.Tile({
            title: 'RoadMaps',
            source: new ol.source.OSM({
                url: 'http://mt{0-3}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',
                attributions: [
                new ol.Attribution({ html: '© Google' }),
                new ol.Attribution({ html: '<a href="https://developers.google.com/maps/terms">Terms of Use.</a>' })
                ]
            })
        }),
        new ol.layer.Tile({
            title: 'Satelite',
            type: 'base',
            visible: false,
            source: new ol.source.OSM({
                url: 'http://mt{0-3}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',
                attributions: [
                new ol.Attribution({ html: '© Google' }),
                new ol.Attribution({ html: '<a href="https://developers.google.com/maps/terms">Terms of Use.</a>' })
                ]
            })
        }),
            new ol.layer.Tile({
            title: 'OSM',
            type: 'base',
            visible: true,
            source: new ol.source.OSM()
        })
        ]
        peta_highlight,
        layers['layer_kategori'],
    })
        ],

        view: new ol.View({
            projection: "EPSG:3857",
        })              
    });

    layerSwitcher = new ol.control.LayerSwitcher({
    tipLabel: 'Légende' // Optional label for button
    });
    map.addControl(layerSwitcher);

此时我的代码仍无效,三张地图无法显示在我的网站上。

1 个答案:

答案 0 :(得分:1)

假设您已正确包含图层切换器库,那么不是导致问题的图层切换器,而是如何定义地图的错误。

peta_higlight之前有一个逗号丢失,layers['layer_kategori'],没有任何意义。

ol.View要求您添加中心和分辨率/缩放。

以下代码应该为您解决这些问题

map = new ol.Map({
    controls: ol.control.defaults({
        attribution: false
    }).extend([new ol.control.MousePosition, new ol.control.ZoomSlider]),
    target: document.getElementById("map"),
    layers: [new ol.layer.Group({
        title: "BaseMaps",
        layers: [
            new ol.layer.Tile({
                title: "RoadMaps",
                source: new ol.source.OSM({
                    url: "http://mt{0-3}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
                    attributions: [new ol.Attribution({
                        html: "© Google"
                    }), new ol.Attribution({
                        html: '<a href="https://developers.google.com/maps/terms">Terms of Use.</a>'
                    })]
                })
            }),
            new ol.layer.Tile({
                title: "Satelite",
                type: "base",
                visible: false,
                source: new ol.source.OSM({
                    url: "http://mt{0-3}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
                    attributions: [new ol.Attribution({
                        html: "© Google"
                    }), new ol.Attribution({
                        html: '<a href="https://developers.google.com/maps/terms">Terms of Use.</a>'
                    })]
                })
            }),
            new ol.layer.Tile({
                title: "OSM",
                type: "base",
                visible: true,
                source: new ol.source.OSM()
            }), peta_highlight
        ]
    })],
    view: new ol.View({
        center: [0, 0],
        zoom: 5,
        projection: 'EPSG:3857'
    })
});

layerSwitcher = new ol.control.LayerSwitcher({
    tipLabel: 'Légende' // Optional label for button
});
map.addControl(layerSwitcher);
相关问题