图层组设置与使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>图层组</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
    <style>
        .map {
            height: 200px;
            width: 100%;
        }
        #layertree li > span {
            cursor: pointer;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
    <meta charset="UTF-8">
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<div id="layertree">
    <h5>Click on layer nodes below to change their properties.</h5>
    <ul>
        <li><span>OSM层</span>
            <fieldset id="layer0">
                <label class="checkbox" for="visible0">
                    <input id="visible0" class="visible" type="checkbox"/>可见度
                </label>
                <label>不透明度</label>
                <input class="opacity" type="range" min="0" max="1" step="0.01"/>
            </fieldset>
        </li>
        <li><span>图层组</span>
            <fieldset id="layer1">
                <label class="checkbox" for="visible1">
                    <input id="visible1" class="visible" type="checkbox"/>可见度
                </label>
                <label>不透明度</label>
                <input class="opacity" type="range" min="0" max="1" step="0.01"/>
            </fieldset>
            <ul>
                <li><span>粮食不安全层</span>
                    <fieldset id="layer10">
                        <label class="checkbox" for="visible10">
                            <input id="visible10" class="visible" type="checkbox"/>可见度
                        </label>
                        <label>不透明度</label>
                        <input class="opacity" type="range" min="0" max="1" step="0.01"/>
                    </fieldset>
                </li>
                <li><span>世界边界层</span>
                    <fieldset id="layer11">
                        <label class="checkbox" for="visible11">
                            <input id="visible11" class="visible" type="checkbox"/>可见度
                        </label>
                        <label>不透明度</label>
                        <input class="opacity" type="range" min="0" max="1" step="0.01"/>
                    </fieldset>
                </li>
            </ul>
        </li>
    </ul>
</div>
<script type="text/javascript">
    var key = 'Your Mapbox access token from https://mapbox.com/ here';

    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({source: new ol.source.OSM()}),
            new ol.layer.Group({
                layers: [
                   new ol.layer.Tile({
                            source: new ol.source.TileJSON({
                            url:'https://api.tiles.mapbox.com/v4/mapbox.20110804-hoa-foodinsecurity-3month.json?secure&access_token=' + key,
                            crossOrigin: 'anonymous'
                        })
                    }),
                    new ol.layer.Tile({
                        source: new ol.source.TileJSON({
                            url:'https://api.tiles.mapbox.com/v4/mapbox.20110804-hoa-foodinsecurity-3month.json?secure&access_token=' + key,
                            crossOrigin: 'anonymous'
                        })
                    })

                ]
            })
        ],
        view: new ol.View({
            // 设置贵阳为地图中心,此处进行坐标转换, 把EPSG:4326的坐标,转换为EPSG:3857坐标,因为ol默认使用的是EPSG:3857坐标
            center: ol.proj.transform([106.709, 26.629], 'EPSG:4326', 'EPSG:3857'),
            zoom: 10
        }),
        target: 'map'
    });

    function bindInputs(layerid, layer) {
        var visibilityInput = $(layerid + ' input.visible');
        visibilityInput.on('change', function() {
            layer.setVisible(this.checked);
        });
        visibilityInput.prop('checked', layer.getVisible());

        var opacityInput = $(layerid + ' input.opacity');
        opacityInput.on('input change', function() {
            layer.setOpacity(parseFloat(this.value));
        });
        opacityInput.val(String(layer.getOpacity()));
    }
    map.getLayers().forEach(function(layer, i) {
        bindInputs('#layer' + i, layer);
        if (layer instanceof ol.layer.Group) {
            layer.getLayers().forEach(function(sublayer, j) {
                bindInputs('#layer' + i + j, sublayer);
            });
        }
    });

    $('#layertree li > span').click(function() {
        $(this).siblings('fieldset').toggle();
    }).siblings('fieldset').hide();
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值