Select to view content in your preferred language

JSON to LayerLegendInfo

3345
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
DasaPaddock
Esri Regular Contributor
You can call getLegendInfos() on the 3 layers that implement the ILegendSupport interface and your responder will get an Array of LayerLegendInfo instances:
http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/ILegendSupport.html
0 Kudos
AndrewHaigh
Occasional Contributor
Dasa,

Thanks for your feedback on this, I did come accross the ILegendSupport interface after I posted this but have only just had time to look into it.

I think I have written some code which should work however, I seem to be getting a rather unexpected result.

Note that I'm using ArcGIS Server 10.0 Service Pack 1 (Microsoft .Net Framework Edition) with ArcGIS API for Flex version 2.2.2010-12-08 on a secure network (i.e. no internet access).

Here's the code, which is invoked once the ArcGISDynamicMapServiceLayer has completed initialisation.

protected function referenceDataLayerInitComplete(event:FlexEvent):void
{
 // Note that referenceDataMapLayer is an ArcGISDynamicMapServiceLayer from 10.0 SP1
 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");
}


Everytime I invoke the code I get a HTTP error because it appears as though the getLegendsInfos is always sending the request to http://www.arcgis.com/sharing/tools/legend - which fails because there is no internet connection.

I have checked the service info returned for the ArcGISDynamicMapServiceLayer and it is returning the correct version of 10.01 so I would expect it to access the local rest endpoint for the legend.

Have i done something wrong or is this a bug.

Thanks

Andrew
0 Kudos
AndrewHaigh
Occasional Contributor
Hmm,

Odd, I seem to have discovered that this works ok but my problem was actually down to an issue with a stale capabilities response been cached by the browser.

Oh all working fine now.
0 Kudos
DanielBradshaw
Deactivated User
A bit old... but what capability were you missing?  I have Map, Query, Data but am seeing the same issue you described.  All getLegendInfos calls always route through the www.arcgis.com which completely fails if my local map service isn't externally accessible.
0 Kudos
AndrewHaigh
Occasional Contributor
Well I'm not totally sure what I did to fix the problem, if I'm honest but here are the steps I completed which seemed to solve the problem:

1. Clear the ArcGIS Server Rest Services Cache (http://server/ArcGIS/rest/admin)

2. Clear the browser Cache

I suspect it was the first change which resolved the problem, but you should be able to check access to the legend via http://server/ArcGIS/rest/services/MapServiceName/MapServer/legend.

Hope this helps

Andrew
0 Kudos
JasonTrimble
Emerging Contributor
I too am having the same issue. My arcgis server is at version 10.01, but when my flex app attempts to call getLegendInfos on a layer the requests is trying to go through arcgis.com to get legend information.
I can view the JSON response for my service through the rest legend endpoint in my browswer, so that seems to be working.
Why are flex calls trying to go through arcgis.com.

I have cleared the rest cache, and my browswer cache, neither of those resolved the issue.
0 Kudos
DanielBradshaw
Deactivated User
I'm with Jason... cleared every concievable cache possible and the getLegendInfos method still insists on routing through arcgis.com
0 Kudos
ReneRubalcava
Esri Frequent Contributor
I ran into similar problem at some point, which I thought was weird.
I ended up just parsing the json result, which is fast and gives me a little freedom to do some other stuff with it as well.
https://github.com/odoe/LegendViewer/blob/master/src/net/odoe/flexmaptools/components/helpers/LayerE...
0 Kudos
DanielBradshaw
Deactivated User
Thanks for the response Rene.  I originally was using the same methods as you (parsing the JSON directly), but the getLegendInfos gives me information about group layers (using the REST enpoint directly doesn't). 

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

At the end of the day, I still couldn't get the flex API to do what I wanted, but I'm satisfied with my workaround for now.
0 Kudos