Select to view content in your preferred language

add remove KMLlayer with ArcGIS API 3 for flex (Flash builder 4.6)

2657
6
09-17-2012 07:51 AM
SaidAkif
Occasional Contributor
Hi

I want to add a kml layer (id=xxx) on my map by selecting a checkbox like following:

        <esri:Map id="map">               
      <esri:KMLLayer id="chargkml"/>                      
        </esri:Map>

       <s:CheckBox id="RSS"     click="RSS_clickHandler()"/>

      protected function RSS_clickHandler():void {
                if (RSS.selected) {
                 chargkml.url = "http://www.mapottawa.ca/data/2009_splash_pads.points.kmz";}
                else {
                xxxxxxxxxxxxxxx  
                       }}

It's ok to add because i just set the url of the kml.
my problem is that i don't know how to remove (clear or delete) my kml when I deseleck the checkbos.

Thanks for your help
Tags (2)
0 Kudos
6 Replies
DasaPaddock
Esri Regular Contributor
You could bind the visible property to the checkbox like this:

<s:CheckBox id="RSS" label="KML Features"/>

<esri:KMLLayer id="chargkml"
                       url="http://www.mapottawa.ca/data/2009_splash_pads.points.kmz"
                       visible="{RSS.selected}"/>
0 Kudos
SaidAkif
Occasional Contributor
Thanks a lot for your answer
It's exactly what I need

sorry, but I have two more questions:
1- With the 'visible', the kml is still charged, How can I delete it?
2- How can I get the kml extend, and zoom the map on it?

Thanks for your help
0 Kudos
IvanBespalov
Frequent Contributor
Try this:
<?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>
0 Kudos
SaidAkif
Occasional Contributor
realy thanks a lot
0 Kudos
rizalosman
New Contributor
Hi akifsaid... Im new in flex and doesn't know about coding. can you this add remove kml widget. I used Arcgis viewer v3.
0 Kudos
rizalosman
New Contributor
Hi akifsaid... Im new in flex and doesn't know about coding. can you share this add remove kml widget. I used Arcgis viewer v3.
0 Kudos