ArcGIS API for JavaScript 实现图层控制树

前言

    控制服务中的图层显隐

思路

    通过MapServiceLayer的setVisibleLayers()方法设置显示的图层的显示

代码实现

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" type="text/css" href="http://localhost:8080/arcgis_js_api/library/3.20/3.20/dijit/themes/tundra/tundra.css"/>
    <link rel="stylesheet" type="text/css" href="http://localhost:8080/arcgis_js_api/library/3.20/3.20/esri/css/esri.css" />
    <script type="text/javascript" src="http://localhost:8080/arcgis_js_api/library/3.20/3.20/init.js"></script>
    <style>
        html, body, #map {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #FFF;
            overflow: hidden;
            font-family: "Trebuchet MS";
        }
    </style>
    <script>
        var map,
            visible=[],
            setLayerVisibility;
        require([
                "esri/map",
                "esri/layers/ArcGISDynamicMapServiceLayer",
                "dojo/on",
                "dojo/dom",
                "dojo/query",
                "dojo/dom-attr",
                "dojo/domReady!"],
            function(Map,
                     ArcGISDynamicMapServiceLayer,
                     on,
                     dom,
                     query,
                     domAttr
                     )
            {
                map = new Map("map", {
                    center: [112, 38],
                    zoom: 5,
                    basemap: "topo"
                });
                var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/firstTest/firstService/MapServer");
                map.addLayer(dynamicMapServiceLayer);


               on(dynamicMapServiceLayer,"load",loadLayerList);

                function loadLayerList(layers){
                    var html="";
                    var infos=layers.layer.layerInfos;
                    for(var i= 0,length=infos.length;i<length;i++){
                        var info = infos[i];
                        //图层默认显示的话就把图层id添加到visible
                        if(info.defaultVisibility)
                        {
                            visible.push(info.id);
                        }
                        //输出图层列表的html
                        html=html+"<div><input id='"+info.id+"' name='layerList' class='listCss' type='checkbox' value='checkbox' onclick='setLayerVisibility()' "+(info.defaultVisibility ? "checked":"")+" />"+info.name+"</div>";
                    }
                    //设置可视图层
                    dynamicMapServiceLayer.setVisibleLayers(visible);
                    //在右边显示图层名列表
                    domAttr.set(dom.byId("toc"),"innerHTML",html);
                    // dom.byId("toc").innerHTML=html;
                }
                setLayerVisibility = function()
                {
                    //用query获取css为listCss的元素数组
                    var inputs = query(".listCss");
                    visible = [];
                    //对checkbox数组进行变量把选中的id添加到visible
                    for(var i=0;i<inputs.length;i++)
                    {
                        if(inputs[i].checked)
                        {
                            visible.push(inputs[i].id);
                        }
                    }
                    //设置可视图层
                    dynamicMapServiceLayer.setVisibleLayers(visible);
                }
            });
    </script>
</head>

<body>
<div id="map">
    <div id="toc" style="position: absolute; left: 10px; bottom: 20px; border: 1px solid #9c9c9c; background: #fff; width: 100px; height: auto; z-index: 99;padding: 10px;"></div>
</div>
</body>
</html>

效果

这里写图片描述

这里写图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值