描述
本例展示如何使用图层定义来限制显示在地图上的图层信息。为了了解本例做了什么,看看用于这个地图的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上设置图层定义。这些图层使用预先渲染的图片切片的缓存。虽然切片地图服务更快,但是被限制在切片上查看地图图片。
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 6 <meta http-equiv="X-UA-Compatible" content="IE=7" /> 7 <title>动态创建图层列表</title> 8 <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css"> 9 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script> 10 <script type="text/javascript"> 11 dojo.require("esri.map"); 12 13 var dynamicMapServiceLayer,map,visible = []; 14 15 function init() { 16 map = new esri.Map("map"); 17 18 dynamicMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer( 19 "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer" 20 ); 21 22 if (dynamicMapServiceLayer.loaded) { 23 buildLayerList(dynamicMapServiceLayer); 24 }else{ 25 dojo.connect(dynamicMapServiceLayer,"onLoad",buildLayerList); 26 } 27 } 28 29 function buildLayerList(layer) { 30 var infos = layer.layerInfos, info; 31 var items = []; 32 for (var i=0,j=infos.length;i<j;i++) { 33 info = infos[i]; 34 if (info.defaultVisibility) { 35 visible.push(info.id); 36 } 37 items[i] = "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" + info.id +"' onclick='updateLayerVisibility();'/><label for='" +info.id+ "'>" +info.name+ "</label>"; 38 } 39 dojo.byId("layer_list").innerHTML = items.join(); 40 41 layer.setVisibleLayers(visible); 42 map.addLayer(layer); 43 44 } 45 46 function updateLayerVisibility() { 47 var inputs = dojo.query(".list_item"), input; 48 visible = []; 49 for (var i=0,j=inputs.length;i<j;i++) { 50 if(inputs[i].checked) { 51 visible.push(inputs[i].id); 52 } 53 } 54 dynamicMapServiceLayer.setVisibleLayers(visible); 55 56 } 57 dojo.addOnLoad(init); 58 59 </script> 60 61 </head> 62 63 <body > 64 This sample loads an ArcGISDynamicMapServiceLayer.<br /> 65 It determines the layers in the map service and presents them as checkboxes that can be used to toggle their visibility.<br /> 66 <br /> 67 Layer List: <span id="layer_list"></span><br /> 68 <br /> 69 <div id="map" class="tundra" style=" 900px;height: 600px;border: 1px solid #000"></div> 70 </body> 71 </html>