描述

本例展示了如何确切地创建一个地图服务里的图层列表。这个列表由HTML checkboxe组成,可用用于开关图层的可见性。

函数updateLayerVisibility()包含开关图层的逻辑。函数循环访问列表里的每个图层,依赖checkbox状态图层是否应该可见,并且使用ArcGISDynamicMapServiceLayer.setVisibleLayers()更新相应图层可见性。

如果需要创建地图服务里的所有图层的图层列表,见示例动态地创建图层列表.

 <%@ 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"); var imageParameters = new esri.layers.ImageParameters();
imageParameters.visibleLayers = [0,1,2]; dynamicMapServiceLayer=new esri.layers.ArcGISDynamicMapServiceLayer(
"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer", {"imageParameters":imageParameters}
);
map.addLayer(dynamicMapServiceLayer);
} function updateLayerVisibility() {
var inputs = dojo.query(".list_item"), input;
//在这个应用中,图层 2是一只可见的
visible = [2];
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 and presents check boxes for only the layers that should be toggled on and off by users. <br />ed to toggle their visibility.<br />
<br />
Layer List: <span id="layer_list"><input type="checkbox" class="list_item" checked="checked" id='0' value=0 onclick="updateLayerVisibility();"/>Cities&nbsp;&nbsp;
<input type="checkbox" class="list_item" checked="checked" id="1" value=1 onclick="updateLayerVisibility();"/>River&nbsp;&nbsp; </span><br />
<br />
<div id="map" class="tundra" style="width: 900px;height: 600px;border: 1px solid #000"></div>
</body>
</html>
05-23 19:23