Select to view content in your preferred language

Data Binding from LayerTOC

3063
11
11-10-2010 03:20 PM
JasonLevine
Deactivated User
Hello All,
    I'm working with the flex sample code (non sample viewer) found in the resource center, specifically with the "Identify" and "Dynamic Map Layers on/off" components.  In my identify function, I've added an identify parameter "identifyParams.layerIds = [0]" to only run the identify on the specified layer in my map service (in the example below, the specified layer is the layer with a layer ID of 0):
var identifyParams:IdentifyParameters = new IdentifyParameters();
    identifyParams.returnGeometry = true;
    identifyParams.tolerance = 3;
    identifyParams.width = myMap.width;
    identifyParams.height = myMap.height;
    identifyParams.geometry = event.mapPoint;
    identifyParams.mapExtent = myMap.extent;
    identifyParams.spatialReference = myMap.spatialReference;
    identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
    identifyParams.layerIds = [0];


In the LayerTOC.mxml, I'd like to create an itemClick function in the DataGrid that does the following:

1) Create a new array variable
2) populates the array with the Layer ID of the selected layer in the TOC

I'd like to then bind the new array variable to my main.mxml and set it as the array for my identifyParams.layerIds array.  Basically, I want to be able to set the layer I'd like to do the identify on by selecting it in the TOC. The concept seems pretty straightforward, but I can't seem to create and bind my array without creating several errors.

Any help is appreciated.

Thanks,
Jason
Tags (2)
0 Kudos
11 Replies
by Anonymous User
Not applicable
Hi Jason, 

This would be user friendly.  Have you tried using a switch and case functionality to store the layerId information in a class / object? 

For example create and then import a simple class / object like below and use a switch / case statement to populate the object.

public class IdentifyHelper
{

   private var _myIds:Array;
   
   public function get myIds():Array
   {

      return this.myIds;

   }
   
   public function set myIds( value:Array ):void
   {

      this.myIds = value;

   }

}

   /* in theory all your identify parameters could go here, if you desire a different look and feel for each layer */





Next in code, set the object property in a switch / case based on the layer you want "active"



switch (myString) {

    case "Rivers":

        identifyhelper.myIds = [0];

        break;

    case "Lakes":

        identifyhelper.myIds = [1];

        break;

    case "Plants":

        identifyhelper.myIds = [2];

        break;

}



Then when it comes time to do the Identify you could "get" your layerId parameters out of the object, as follows.

identifyParams.layerIds = identifyhelper.myIds;


You could also create a simple object at the global scope and not go with a full blown class with getters and setters.  Down the road, though, I think the above logic would make for a strong framework.  It's a little more extensible and could be recycled in other projects. 

Regards,
Doug Carroll, ESRI Support Services SDK Team
http://support.esri.com/
0 Kudos
JasonLevine
Deactivated User
Hi Doug,
   Thanks for this idea, I think it'll work nicely. However, I'm running into some errors while compiling. 

I've created and imported my IdentifyHelper.as class:
package com.esri.ags.script 
{
 public class IdentifyHelper
 {
  private var _myIds:Array;
  public function get myIds():Array
  {
   return this.myIds;
  }
  public function set myIds( value:Array ):void
  {
   this.myIds = value;
  }
 }
}


Then, in my LayerTOC.mxml, I've created an itemClick function in the DataGrid:
private function onItemClick(event):void
   {
    switch (this.selectedItem.name)
    {
     case "Zoning":
      IdentifyHelper.myIds = [4];
      break;
     case "LUP - Area Comm Plan":
      IdentifyHelper.myIds = [5];
      break;
     case "LUP - General Plan":
      IdentifyHelper.myIds = [6];
      break;
    }
   }


And in my Main.mxml, I've included the IdentifyHelper.myIds:
protected function mapClickHandler(event:MapMouseEvent):void
   {
    clickGraphicsLayer.clear();
    graphiclayer.clear();
    
    var identifyParams:IdentifyParameters = new IdentifyParameters();
    identifyParams.returnGeometry = true;
    identifyParams.tolerance = 3;
    identifyParams.width = map.width;
    identifyParams.height = map.height;
    identifyParams.geometry = event.mapPoint;
    identifyParams.mapExtent = map.extent;
    identifyParams.spatialReference = map.spatialReference;
    identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
    identifyParams.layerIds = IdentifyHelper.myIds;
    var clickGraphic:Graphic = new Graphic(event.mapPoint, clickPtSym);
    clickGraphicsLayer.add(clickGraphic);
    
    identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic));
   }


The problem I'm having is that everywhere in my code where I reference "IdentifyHelper.myIds"
I get the following error:

1119:Access of possibly undefined property myIds through a reference with static type Class.

Is this an issue of casting?

Thanks,
Jason
0 Kudos
JasonLevine
Deactivated User
Anyone know how to resolve this error?
0 Kudos
DasaPaddock
Esri Regular Contributor
It looks like you need to create and use an instance of IdentifyHelper.
0 Kudos
JasonLevine
Deactivated User
Hello Dasa,
   So I've created an instance of IdentifyHelper here:

public static var identify:IdentifyHelper = new IdentifyHelper;
private function onItemClick(event):void
   {
    switch (this.selectedItem.name)
    {
     case "Zoning":
      identify.myIds = [4];
      break;
     case "LUP - Area Comm Plan":
      identify.myIds = [5];
      break;
     case "LUP - General Plan":
      identify.myIds = [6];
      break;
    }
   }


And referenced it here:
protected function mapClickHandler(event:MapMouseEvent):void
   {
    clickGraphicsLayer.clear();
    graphiclayer.clear();
    
    var identifyParams:IdentifyParameters = new IdentifyParameters();
    identifyParams.returnGeometry = true;
    identifyParams.tolerance = 3;
    identifyParams.width = map.width;
    identifyParams.height = map.height;
    identifyParams.geometry = event.mapPoint;
    identifyParams.mapExtent = map.extent;
    identifyParams.spatialReference = map.spatialReference;
    identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
    identifyParams.layerIds = com.esri.ags.samples.LayerTOC.identify.myIds;
    var clickGraphic:Graphic = new Graphic(event.mapPoint, clickPtSym);
    clickGraphicsLayer.add(clickGraphic);
    
    identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic));
   }


This resolved the errors, but now my identify tool wont work at all.  Any ideas?

Thanks,
Jason
0 Kudos
DasaPaddock
Esri Regular Contributor
The code looks OK. Try running the debugger and checking that the values are what you expect. You can also try a tool like HttpFox to see what's being sent to the server.
0 Kudos
JasonLevine
Deactivated User
Hi Dasa,
   I've been scouring over my code and everything looks OK to me as well; The only think I can think of is that the array isn't being populated with any values at all and this is what is causing the identify tool to not work.  How can I check to see what is being loaded into the myIds array when I click on a layer in the TOC?  Is there a way to just trace out the array somewhere so I can at least verify that the values are being loded and are changing as I click layers in the TOC?

Thanks,
Jason
0 Kudos
JasonLevine
Deactivated User
Ok, I debugged, and I receive the following ArgumentError as soon as I click the map:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
 at flash.display::DisplayObjectContainer/getChildIndex()
 at mx.managers::SystemManager/getChildIndex()
 at mx.managers.systemClasses::ActiveWindowManager/mouseDownHandler()


How would I go about fixing this?

Thanks,
Jason
0 Kudos
JasonLevine
Deactivated User
Ok, I've discovered that the ArgumentError was a result of nesting Applications within my main Application;  changing the main <S:Application> to <S:Group> in all of my components fixed this.

Back to the main issue.... When debugging, Flash Builder throws an error when I try to select a layer from my TOC list:

undefined
 at com.esri.ags.script::IdentifyHelper/set myIds()


This corresponds to the line 12 (myIds = value ) of my IdentifyHelper.as file:
package com.esri.ags.script 
{
 public class IdentifyHelper
 {
  private var _myIds:Array;
  public function get myIds():Array
  {
   return myIds;
  }
  public function set myIds(value:Array):void
  {
   myIds = value;
  }
 }
}


Is there something wrong with my IdentifyHelper.as code at that line?

Thanks for your help,
Jason
0 Kudos