首页 > 代码库 > 01_设置定义表达式控制地图内容显示

01_设置定义表达式控制地图内容显示

技术分享

技术分享

本例展示如何使用图层定义来限制显示在地图上的图层信息。为了了解本例做了什么,看看用于这个地图的ESRI_Census_USA服务的服务目录页是有帮助的。检查地图中的图层列表。现在注意这行代码限制了显示图层为州和县。

dynamicMapServiceLayer.setVisibleLayers([5,4,3]);

这个数据覆盖了美国,那么为什么当运行示例时仅仅Kansas周被显示?这是因为图层定义,通过SQL表达式限制数据在地图上的显示。本例中,图层定义限制了州为Kansas并且Kansas中的区县人口超过25000。

下面的代码增加图层定义到数组,然后使用 ArcGISDynamicMapServiceLayer.setLayerDefinitions()方法应用这些定义。注意在数组中,定义的索引和地图中图层的索引匹配:

var layerDefs = [];
layerDefs[5] = "STATE_NAME=‘Kansas‘";
layerDefs[4] = "STATE_NAME=‘Kansas‘ and POP2007>25000";
layerDefs[3] = "STATE_NAME=‘Kansas‘ and POP2007>25000";

dynamicMapServiceLayer.setLayerDefinitions(layerDefs);

关于建立SQL表达式的帮助资源,见ArcGIS Desktop帮助里的About building an SQL expression 。

注意不能在 ArcGISTiledMapServiceLayers上设置图层定义。这些图层使用预先渲染的图片切片的缓存。虽然切片地图服务更快,但是被限制在切片上查看地图图片。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>动态创建图层列表</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
    <script type="text/javascript">
        dojo.require("esri.map");

        var dynamicMapServiceLayer, map, visible = [];

        function init() {
            map = new esri.Map("map");

            dynamicMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer(
                "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"
            );

            if (dynamicMapServiceLayer.loaded) {
                buildLayerList(dynamicMapServiceLayer);
            } else {
                dojo.connect(dynamicMapServiceLayer, "onLoad", buildLayerList);
            }
        }

        function buildLayerList(layer) {
            var infos = layer.layerInfos, info;
            var items = [];
            for (var i = 0, j = infos.length; i < j; i++) {
                info = infos[i];
                if (info.defaultVisibility) {
                    visible.push(info.id);
                }
                items[i] = "<input type=‘checkbox‘ class= ‘list_item‘ checked=‘" + (info.defaultVisibility ? "checked" : "") + "‘ id=‘" + info.id + "‘ onclick=‘updateLayerVisibility();‘/><label for=‘" + info.id + "‘>" + info.name + "</label>";
            }
            dojo.byId("layer_list").innerHTML = items.join();

            layer.setVisibleLayers(visible);
            map.addLayer(layer);

        }

        function updateLayerVisibility() {
            var inputs = dojo.query(".list_item"), input;
            visible = [];
            for (var i = 0, j = inputs.length; i < j; i++) {
                if (inputs[i].checked) {
                    visible.push(inputs[i].id);
                }
            }
            dynamicMapServiceLayer.setVisibleLayers(visible);

        }
        dojo.addOnLoad(init);

    </script>

</head>

<body>
    This sample loads an ArcGISDynamicMapServiceLayer.<br />
    It determines the layers in the map service and presents them as checkboxes that can be used to toggle their visibility.<br />
    <br />
    Layer List: <span id="layer_list"></span><br />
    <br />
    <div id="map" class="tundra" style="width: 900px;height: 600px;border: 1px solid #000"></div>
</body>
</html>

01_设置定义表达式控制地图内容显示