<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags"> <!-- Adobe SDK 4.6 --> <!-- ArcGIS API 3.0 --> <s:layout> <s:HorizontalLayout gap="10" paddingLeft="20" paddingTop="20" paddingBottom="20" paddingRight="20"/> </s:layout> <fx:Script> <![CDATA[ import com.esri.ags.events.LayerEvent; import com.esri.ags.geometry.Extent; import com.esri.ags.layers.KMLLayer; import com.esri.ags.layers.Layer; import com.esri.ags.utils.JSONUtil; import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; protected function onCheckBoxChange(event:Event):void { trace(">>>\nBox state chnaged"); var box:CheckBox = CheckBox(event.target); var url:String = box.name; if (box.selected) { // create new kml layer here var kmlLayer:KMLLayer = new KMLLayer(url); kmlLayer.addEventListener(LayerEvent.LOAD, onLayerLoad); map.addLayer(kmlLayer); // add to map } else { var layers:ArrayCollection = ArrayCollection(map.layers); // find and remove unchecked layer in map layers collection for each (var layer:Layer in layers) { if (layer is KMLLayer && KMLLayer(layer).url == url) { map.removeLayer(layer); break; } } } trace(">>>\nMap layers count = " + map.layerIds.length); } protected function onLayerLoad(event:LayerEvent):void { trace(">>>\nLayer loaded"); var layer:KMLLayer = event.target as KMLLayer; layer.removeEventListener(LayerEvent.LOAD, onLayerLoad); // get kml layer details var httpService:HTTPService = new HTTPService(); httpService.url = layer.serviceURL + "?url=" + layer.url; httpService.method = URLRequestMethod.POST; httpService.showBusyCursor = true; httpService.resultFormat = "text"; httpService.concurrency = "last"; httpService.addEventListener(ResultEvent.RESULT, httpServiceResultFunction); //TODO add fault listener here if needed httpService.send(); } protected function httpServiceResultFunction(event:ResultEvent):void { if (event.result) { try { var resultObj:Object = JSONUtil.decode(event.result.toString()); // get layer extent and zoom to it if (resultObj.hasOwnProperty("lookAtExtent")) { var lookAtExtent:Object = resultObj.lookAtExtent; map.initialExtent = Extent.fromJSON(lookAtExtent); map.zoomToInitialExtent(); } } catch (error:Error) { trace(">>>\nError parsing result"); } } } ]]> </fx:Script> <s:VGroup height="100%" width="200"> <s:CheckBox label="2009 splash pads" name="http://www.mapottawa.ca/data/2009_splash_pads.points.kmz" selected="false" change="onCheckBoxChange(event)"/> <s:CheckBox label="2010 museums" name="http://www.mapottawa.ca/data/2010_museums.points.kmz" selected="false" change="onCheckBoxChange(event)"/> </s:VGroup> <esri:Map id="map"> <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/> </esri:Map> </s:Application>