Select to view content in your preferred language

JSON to LayerLegendInfo

3360
13
12-16-2010 04:53 AM
AndrewHaigh
Occasional Contributor
I'm wondering what the best way is to convert the results of the ArcGIS Server 10 SP1 Legend request into an array of LayerLegendInfo for an ArcGISDynamicMapServiceLayer.

Is there a smart way I can do it or is it just a mater of iterating through the object returned by the HTTP Service?

I did try and specify a format of AMF onto the legend request but it comes back as not supported.

Thanks

Andrew
Tags (2)
0 Kudos
13 Replies
JasonLevine
Deactivated User

That being said... your suggestion inspired me to work up a new strategy for handling group layers within my application using the REST enpoint.


Hi Daniel
Would you mind sharing your new strategy for this?  I'm working on the same thing right now.

Thanks,
Jason
0 Kudos
DanielBradshaw
Deactivated User
No prob...

Disclaimer:  This app is a custom app, not built within the flex template.

I have a property in my config file for each layer in my app called legendGroup.  This property contains a comma delimited list of layerIds that participate in the "group".  For example, say I have a group layer in my MXD with a layerId of 5, and it's children are layers 6, 7, 8.  Each of the children have scale dependencies such that only one of them is visible at any given time and I want my legend to reflect only the visible layer.  The layer in my config file would have a legendGroup property = "6,7,8".

While building my legend, I pass the legendGroup property and current map scale of the layer of interest to my legend class.  I then loop through the JSON results from the legend REST endpoint and find all the layerIds that match one of the layers within my legendGroup.  I then loop through the 3 layers that match and compare the legend scale to the map scale I originally passed in.  At this point I can confidently pass the "appropriate" legend data back for further processing.

The cons to this solution is you have to manually configure your application to tell it which layers participate in the legend group... but on the flip side you can add as many layers to the group as you want whether they participate in a group layer in the MXD or not, and you always get the legend data for the visible layer in the group.

Make sense?
0 Kudos
JasonTrimble
Emerging Contributor
With the help of Tech Support we were able to determine the issue I was having had to do with the timing of when I was calling the getLayerLegendInfo() method.

I was dynamically adding a layer to our map in actionscript and then immediately I would call the getLayerLegendInfo() method to store off the legend data for that layer. The problem is, the layer has not completely loaded at that point. Instead you have to register an event listener for LOAD LayerEvent. When that event fires you can then call the getLayerLegendInfo() method and all the legend data is populated.

The reason the requests attempt to proxy thru arcgis.com is that the underlying legend data retrieval code must look at the version property on the layer. That version property is NaN until the layer has loaded completely, once loaded it is 10.01, which signals to not use the arcgisl.com service for legend data.

Jason
0 Kudos
AndrewHaigh
Occasional Contributor
Jason,

I can confirm this is correct, I obviously copied an old version of my code onto the forums - sorry everyone!

The correct code snippet is below:

protected function referenceDataLayerLoadComplete(event:FlexEvent):void
{
 // Note that referenceDataMapLayer is an ArcGISDynamicMapServiceLayer from 10.0 SP1
        // we fire the event on the layer LOAD event, otherwise we end up called arcgis.com :(
 var token:AsyncToken = referenceDataMapLayer.getLegendInfos(null);
 var responder:mx.rpc.Responder = new mx.rpc.Responder(getLegendResult, getLegendFault);
 token.addResponder(responder);
}

public function getLegendResult(obj:Object):void
{
 trace("We have a result");
}

public function getLegendFault(obj:Object):void
{
 trace("We have an error");
}


Hope this helps everyone

Andrew
0 Kudos