ArcGIS For Flex学习之Layers---控制图层的显示与否(利用DataGrid)

这个例子演示的是通过右侧DataGrid中的复选框来控制相应图层的可见性,由于ArcGISDynamicMapServiceLayer的defaultVisibility属性在地图发布时均为可见(true),所以开始时复选框均为选中状态,注意一下几点:(1)DataGrid的数据源为图层的LayerInfos(其中包括defaultVisibility、layerId、name等,可以用作某些控件的dataField属性);(2)复选框的select以及click属性;(3)复选框在选中状态时,visibleLayers = dynamicLayer.visibleLayers,visibleLayers.addItem(layerInfo.layerId)将该图层的Id号传过去,具体效果图及代码如下所示:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
 3                xmlns:s="library://ns.adobe.com/flex/spark"
 4                xmlns:esri="http://www.esri.com/2008/ags"
 5                xmlns:samples="com.esri.ags.samples.*"
 6                backgroundColor="0xEEEEEE"
 7                pageTitle="Turning individual layers on/off for an dynamic ArcGIS map service" xmlns:samples1="samples.*">
 8
 9     <s:layout>
10         <s:HorizontalLayout/>
11     </s:layout>
12
13     <fx:Script>
14         <![CDATA[
15             import com.esri.ags.events.LayerEvent;
16
17             import mx.controls.Alert;
18             import mx.rpc.events.FaultEvent;
19
20             protected function myDynamicService_loadErrorHandler(event:LayerEvent):void
21             {
22                 Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "Layer Load Error " + event.fault.faultCode);
23             }
24
25             protected function myDynamicService_faultHandler(event:FaultEvent):void
26             {
27                 Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "Layer Fault " + event.fault.faultCode);
28             }
29         ]]>
30     </fx:Script>
31
32     <s:controlBarLayout>
33         <s:VerticalLayout gap="10"
34                           paddingBottom="7"
35                           paddingLeft="10"
36                           paddingRight="10"
37                           paddingTop="7"/>
38     </s:controlBarLayout>
39     <s:controlBarContent>
40         <s:RichText width="100%">
41             This sample demonstrates how you can use a custom Table of Contents (TOC) component
42             to allow end-users to easily turn on/off layers for any dynamic ArcGIS map service.
43         </s:RichText>
44         <s:Label fontWeight="bold" text="URL of a dynamic (not cached) Map Service:"/>
45         <s:HGroup width="100%">
46             <s:TextInput id="myURL"
47                          width="95%"
48                          creationComplete="myDynamicService.url = myURL.text"
49                          enter="myDynamicService.url = myURL.text"
50                          text="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer"/>
51             <s:Button click="myDynamicService.url = myURL.text" label="Load"/>
52         </s:HGroup>
53     </s:controlBarContent>
54
55     <esri:Map wrapAround180="true">
56         <esri:extent>
57             <esri:Extent xmin="-14408162" ymin="3592860" xmax="-11130542" ymax="5309942">
58                 <esri:SpatialReference wkid="102100"/>
59             </esri:Extent>
60         </esri:extent>
61         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer"/>
62         <esri:ArcGISDynamicMapServiceLayer id="myDynamicService"
63                                            fault="myDynamicService_faultHandler(event)"
64                                            load="myDynamicService.visibleLayers = null"
65                                            loadError="myDynamicService_loadErrorHandler(event)"/>
66     </esri:Map>
67     <samples1:LayerTOC width="210" height="100%"
68                       mapLayer="{myDynamicService}"/>
69
70 </s:Application>

Application主程序

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <!-- Used by ArcIMS_TOC.mxml and Dynamic_TOC.mxml -->
  3 <s:DataGrid xmlns:fx="http://ns.adobe.com/mxml/2009"
  4             xmlns:s="library://ns.adobe.com/flex/spark"
  5             width="100%" height="100%"
  6             resizableColumns="false"
  7             sortableColumns="false">
  8
  9     <fx:Script>
 10         <![CDATA[
 11             import com.esri.ags.events.LayerEvent;
 12             import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
 13             import com.esri.ags.layers.Layer;
 14             import com.esri.ags.layers.supportClasses.LayerInfo;
 15
 16             import mx.collections.ArrayList;
 17             import mx.collections.IList;
 18             import mx.utils.ObjectUtil;
 19
 20             private var _layerInfos:Array;
 21
 22             private var _layer:Layer;
 23             private var _layerChanged:Boolean;
 24             //******************这里自定义mapLayer属性*******************
 25             public function get mapLayer():Layer
 26             {
 27                 return _layer;
 28             }
 29
 30             public function set mapLayer(value:Layer):void
 31             {
 32                 _layer = value;
 33                 _layerChanged = true;
 34                 invalidateProperties();
 35             }
 36             //**********************************************************
 37
 38
 39             override protected function commitProperties():void
 40             {
 41                 if (_layerChanged)
 42                 {
 43                     _layerChanged = false;
 44                     if (mapLayer)
 45                     {
 46                         mapLayer.addEventListener(LayerEvent.UPDATE_END, removeBusyCursor, false, 0, true);
 47                         if (mapLayer.loaded)
 48                         {
 49                             setDataProvider();
 50                         }
 51                         else
 52                         {
 53                             mapLayer.addEventListener(LayerEvent.LOAD, layerLoadHandler, false, 0, true);
 54                         }
 55                     }
 56                 }
 57
 58                 // calling this last fixes an issue when the mapLayer is set late via AS
 59                 super.commitProperties();
 60             }
 61
 62             private function setDataProvider():void
 63             {
 64                 if (mapLayer is ArcGISDynamicMapServiceLayer)
 65                 {
 66                     _layerInfos = ArcGISDynamicMapServiceLayer(mapLayer).layerInfos;
 67                 }
 68                 if (_layerInfos)
 69                 {
 70                     // make sure copy has typed LayerInfo objects
 71                     registerClassAlias("com.esri.ags.layers.supportClasses.LayerInfo", LayerInfo);
 72                     // create a copy since it‘ll be modified
 73                     _layerInfos = ObjectUtil.copy(_layerInfos) as Array;
 74
 75                     // remove group layers and correct defaultVisibility
 76                     if (mapLayer is ArcGISDynamicMapServiceLayer)
 77                     {
 78                         var visibleLayers:Array = getDefaultVisibleLayers();
 79                         var layerInfos:Array = _layerInfos.concat(); // create a shallow clone
 80                         _layerInfos = [];
 81                         for each (var layerInfo:LayerInfo in layerInfos)
 82                         {
 83                             if (!layerInfo.subLayerIds) // skip group layers
 84                             {
 85                                 layerInfo.defaultVisibility = visibleLayers.indexOf(layerInfo.layerId) != -1;
 86                                 _layerInfos.push(layerInfo);
 87                             }
 88                         }
 89                     }
 90
 91                     dataProvider = new ArrayList(_layerInfos);  //在此设置动态地图服务的LayerInfos为DataGrid的数据源
 92                 }
 93             }
 94
 95             private function getDefaultVisibleLayers():Array
 96             {
 97                 var result:Array = [];
 98
 99                 for each (var layerInfo:LayerInfo in _layerInfos)
100                 {
101                     if (layerInfo.parentLayerId >= 0 && result.indexOf(layerInfo.parentLayerId) == -1)
102                     {
103                         // layer is not visible if it‘s parent is not visible
104                         continue;
105                     }
106                     if (layerInfo.defaultVisibility)
107                     {
108                         result.push(layerInfo.layerId);
109                     }
110                 }
111
112                 return result;
113             }
114
115             private function layerLoadHandler(event:LayerEvent):void
116             {
117                 setDataProvider();
118             }
119             //******************************开关图层主要代码***********************************
120             public function showLayer(layerInfo:LayerInfo):void
121             {
122                 var visibleLayers:IList;
123                 if (mapLayer is ArcGISDynamicMapServiceLayer)
124                 {
125                     var dynamicLayer:ArcGISDynamicMapServiceLayer = mapLayer as ArcGISDynamicMapServiceLayer;
126                     visibleLayers = dynamicLayer.visibleLayers;
127                     if (visibleLayers)
128                     {
129                         visibleLayers.addItem(layerInfo.layerId); // add id
130                     }
131                     else
132                     {
133                         visibleLayers = dynamicLayer.visibleLayers = getDynamicVisibleLayers();
134                     }
135                 }
136                 if (visibleLayers)
137                 {
138                     cursorManager.setBusyCursor();
139                 }
140             }
141
142             public function hideLayer(layerInfo:LayerInfo):void
143             {
144                 var visibleLayers:IList;
145                 if (mapLayer is ArcGISDynamicMapServiceLayer)
146                 {
147                     var dynamicLayer:ArcGISDynamicMapServiceLayer = mapLayer as ArcGISDynamicMapServiceLayer;
148                     visibleLayers = dynamicLayer.visibleLayers;
149                     if (visibleLayers)
150                     {
151                         var idIndex:int = visibleLayers.getItemIndex(layerInfo.layerId);
152                         if (idIndex != -1)
153                         {
154                             visibleLayers.removeItemAt(idIndex);
155                         }
156                     }
157                     else
158                     {
159                         visibleLayers = dynamicLayer.visibleLayers = getDynamicVisibleLayers();
160                     }
161                 }
162                 if (visibleLayers)
163                 {
164                     cursorManager.setBusyCursor();
165                 }
166             }
167             //********************************************************************************************
168
169             private function getDynamicVisibleLayers():IList
170             {
171                 var result:ArrayList = new ArrayList();
172
173                 for each (var layerInfo:LayerInfo in _layerInfos)
174                 {
175                     if (layerInfo.defaultVisibility)
176                     {
177                         result.addItem(layerInfo.layerId);
178                     }
179                 }
180                 return result;
181             }
182
183             private function removeBusyCursor(event:Event):void
184             {
185                 cursorManager.removeBusyCursor();
186             }
187         ]]>
188     </fx:Script>
189
190     <s:columns>
191         <s:ArrayList>
192             <s:GridColumn width="70"
193                           headerText="Visibility"
194                           itemRenderer="samples.LayerVizRenderer"/>
195             <s:GridColumn dataField="layerId" headerText="Layer Name"/>
196         </s:ArrayList>
197     </s:columns>
198
199 </s:DataGrid>

LayerTOC.mxml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- Used by LayerTOC.mxml -->
 3 <s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
 4                     xmlns:s="library://ns.adobe.com/flex/spark"
 5                     clipAndEnableScrolling="true">
 6     <fx:Script>
 7         <![CDATA[
 8             import com.esri.ags.layers.supportClasses.LayerInfo;
 9
10             private function cb_clickHandler(event:MouseEvent):void
11             {
12                 var layerInfo:LayerInfo = LayerInfo(data);
13
14                 if (cb.selected)
15                 {
16                     layerInfo.defaultVisibility = true;
17                     LayerTOC(grid.dataGrid).showLayer(layerInfo);
18                 }
19                 else  //初始时,点击复选框,cb.selected = false,执行下面的代码
20                 {
21                     layerInfo.defaultVisibility = false;
22                     LayerTOC(grid.dataGrid).hideLayer(layerInfo);
23                 }
24             }
25         ]]>
26     </fx:Script>
27
28     <s:CheckBox id="cb"
29                 click="cb_clickHandler(event)"
30                 horizontalCenter="0"
31                 selected="{LayerInfo(data).defaultVisibility}"/>
32     <!--defaultVisibility属性是指原始发布的地图服务所设置的图层可见性-->
33 </s:GridItemRenderer>

LayerVizRenderer.mxml

转载地址:https://developers.arcgis.com/flex/sample-code/dynamic-map-layers-on-off.htm

时间: 2024-10-29 19:10:09

ArcGIS For Flex学习之Layers---控制图层的显示与否(利用DataGrid)的相关文章

arcgis for flex 学习笔记(一)

初步认识 地图由图层.要素.样式等组成.地图上有N个图层,图层上有N个要素,每个要素可以存放点.线.面等,每个要素可以设置样式,如果显示图片.或文字均可以先创建一个mxml组件,然后设置到要素上. 面和线都是由点组成的. 添加点 1.首先初始化图层,GraphicsLayer. 2.获取坐标点,MapPoint. 3.创建要素,Graphic. 4.设置样式 Symbol. 5.添加要素至Layer,即是第一步创建的图层. 添加线 在添加点的基础上多了一个步骤. 获取到多个点,可以实例化一个线对

ArcGIS For Flex学习之Mapping---Switching Basemaps

今天开始系统的学习ArcGIS For Flex,先从ESRI的例子学起 1 <?xml version="1.0" encoding="utf-8"?> 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 3 xmlns:s="library://ns.adobe.com/flex/spark" 4 xmlns:esri="http:

ArcGIS For Flex学习之Mapping---Select and zoom

Tips:这个例子说明如何在图层中选择或反选几何对象,当点击地图时,触发mapClick事件,激活QueryTask,这里的Query中的query.geometry = event.mapPoint;利用鼠标点击的点来查找包括该选择点的Polygon,然后将查询到的几何要素添加到GraphicsLayer中并放大一个级别:同时,再次点击选择的要素时,触发unselectGraphic事件,并从GraphicsLayer中移除该要素.具体效果图及代码如下: 1 <?xml version="

ArcGIS for Android学习(一)

GIS的开发中,什么时候都少不了地图操作.ArcGIS for Android中,地图组件就是MapView,MapView是基于Android中ViewGroup的一个类(参考),也是ArcGIS Runtime SDK for Android中的地图容器,与很多ArcGIS API中的Map.MapControl类的作用是一样的. 地图常见的操作有缩放.旋转.平移.获取范围.比例尺.分辨率等信息,以及常用的手势操作,其中,经常使用到的功能和常见问题有以下几个: 1)将地图缩放到指定的比例尺/

arcgis for flex api version3.7 教程:8.创建地图

地图概述: arcgis for flex api提供了大量的方法来实现地图操作和交互.首先,地图(map)支持添加各种类型的图层或服务.图层数据来源可以是arcgis server.Bing maps.graphics layers等.此外还有可以使用网络公开的其他服务像arcgis.com或自建的.网络地图服务有相似的一面,都通过一定的方法支持各种类型的图层 ,任务和数据(数据可以扩展,例如可以是.csv和.gpx文件作为一个组合).下面我们将对此做详细讨论. 创建地图并设置显示范围和空间参

arcgis api 4.x for js之图层管理篇

上篇实现了基础地图加载以及二三维模式切换:本篇的内容则是图层管理控制,从两个不同角度来实现,分别是直接绑定arcgis api提供的图层管理widget(LayerList)以及自定义图层管理图标的点击事件来控制图层的隐藏或者显示.不管是LayerList或者自定义图标点击管理图层的实现方式,核心都是根据Layer的属性visible来设置true或者false来控制的. 一.谈谈本篇自己认为比较重要的几个专业术语名称的理解: 1.TileLayer TileLayer,加载缓存地图服务的瓦片图

STM8S学习笔记-时钟控制1

1.图13可见,STM8S单片机主要有四种时钟源可供选择: 1).1-24MHz外部晶体振荡器(HSE). 2).最大24MHz外部时钟(HSE ext). 3).16MHz高速内部RC振荡器(HSI). 4).128KHz低速内部RC振荡器(LSI). 2.主时钟可以从这四种时钟源中任意切换,切换过程可通过自动切换和手动切换. 自动切换过程: CLK_SWCR |= 0x20;    //使能切换 CLK_SWR = 0xE1; //  (HSI----复位值) // (0xD2 ----LS

【转】【Flex】FLEX 学习网站分享

[转:http://hi.baidu.com/tanghecaiyu/item/d662fbd7f5fbe02c38f6f764 ] FLEX 学习网站分享 http://blog.minidx.com/flex核心开发技术:http://blog.csdn.net/mervyn_lee/archive/2008/10/07/3027039.aspxfl部落:http://www.fltribe.com/捕鱼者说http://www.cnblogs.com/fishert/category/85

AE控制图层中要素可见状态的几种方法

转自原文 AE控制图层中要素可见状态的几种方法 工作中常有这样的需求,一个作业图层由几个作业员来操作,我们要 控制每一个作业员只能看到他负责的区域.作业员的可见区域控制有时候是按空间区域划分,有时候是按照作业属性划分,有时候是属性和区域结合来划分,在程序中应该如何控制呢?本人总结了如下几种可用的方法,不知大家是否有更好的解决方案. ?   唯一值符号法 该方法比较简单,就是通过给图层设置一个唯一值符号渲染,把不想显示的要素符号设置为空.虽然简单,这种方法有这很大的局限性,如果我们要控制某一个区域