Dynamic URI for map layer(s)?

571
5
11-27-2013 05:28 AM
DanielMauer
New Contributor II
I'm trying to figure out if it's possible to load map layers in my ArcGIS Flex API flex application via something like a plaintext config file at runtime.  Basically, I'm debugging my application in one environment using the public ESRI-provided map layers, and then when I deploy the application, it'll be using an internally-hosted ArcGIS server instance on site.  I'd like to be able to let the administrator configure the URIs for the relevant map layers without having to recompile the .swf.  Is that doable?

Thanks!
-Dan
Tags (2)
0 Kudos
5 Replies
Drew
by
Regular Contributor
I'm trying to figure out if it's possible to load map layers in my ArcGIS Flex API flex application via something like a plaintext config file at runtime.  Basically, I'm debugging my application in one environment using the public ESRI-provided map layers, and then when I deploy the application, it'll be using an internally-hosted ArcGIS server instance on site.  I'd like to be able to let the administrator configure the URIs for the relevant map layers without having to recompile the .swf.  Is that doable?

Thanks!
-Dan


This is exactly what the ArcGIS Viewer For Flex does. It has separate config files that allow you to add layers and widgets through XML.  If you download the source, you can see how it is all done.
Source:
https://github.com/Esri/arcgis-viewer-flex/tags


Basically you need to do a HTTP call to the XML file, serialize the file into an object and add the map layers programatically (not through MXML, but through actionscript.

that should start you off.

Drew
0 Kudos
KenBuja
MVP Honored Contributor
This is how I've set up one of my applications. I have one code base that runs several different projects, with each project using the same URL but with a separate parameter at the end.

http://maps.coastalscience.noaa.gov/biomapper/biomapper.html?id=BUIS
http://maps.coastalscience.noaa.gov/biomapper/biomapper.html?id=StJohn

The app uses the parameter to get the information from the XML configuration file to add the different map layers needed for the project (as well as the text in all the information dialogs). Through this configuration file, I can change from our development to production server merely by changing the service node in the XML file without recompiling the swf.

I read the configuration file in my init function like this

private function init():void
{
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE,init_onComplete);
    xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,test);
    xmlLoader.load(new URLRequest("parameters.xml")); //this is the configuration file


and start using it like this
private function init_onComplete(event:Event):void
{
    var params:Object;
    var baseImagery:String;
    var layerBaseImagery:ArcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer;

    try
    {
        var loader:URLLoader = URLLoader(event.target);
        xmlParameters = new XML(loader.data);
        
        params = getURLParameters();
        if (params["id"])
        {
            input = params.id
        }
        else
        {
            Alert.show("Parameter search failed");
            return;
        }
   
        xmlProjectParameters = xmlParameters.project.(@urlinput==input);
        var bm:IBrowserManager = BrowserManager.getInstance();
        bm.init("", xmlProjectParameters.@id + " BIOMapper");

        ///etc.
}

private function getURLParameters():Object
{
    var result:URLVariables = new URLVariables();
    
    try
    {
        if (ExternalInterface.available)
        {
            var search:String = ExternalInterface.call("location.search.substring", 1);
            if (search && search.length > 0)
            {
                result.decode(search);
            }
        }
    }
    catch (error:Error)
    {
        Alert.show(error.toString());
    }
    
    return result;
}


Through configuration file, I load dynamic or tiled layers

        baseImagery = xmlProjectParameters.baseimagery;
        layerBaseImagery.url = serverName + baseImagery + "/MapServer";
        mainMap.addLayer(layerBaseImagery);


This is what the configuration file looks like

<?xml version="1.0" encoding="utf-8"?>
<projects>

  <project id="Buck Island" urlinput="BUIS">
    <projectname name="Buck Island, USVI" pdftitle="Buck Island, USVI: Shallow-Depth Benthic Habitats" url="http://ccma.nos.noaa.gov/ecosystems/coralreef/stcroix.aspx" projectyear="2011"/>
    <service>http://egisws02.nos.noaa.gov/ArcGIS/rest/services/biomapper/BUIS_</service>
    <baseimagery>Imagery</baseimagery>
0 Kudos
DanielMauer
New Contributor II
Thank you both for the informative info, and particularly thanks Ken for the code.  That ought to help me a great deal.

Best,
-Dan
0 Kudos
ScottDawson
New Contributor
Depending on how you're launching the Flex app you could send it in as a param as:

In the web page where Flex is launched for example you could have something like.....
AC_FL_RunContent("src", "${resource(dir:'flex',file:'Footprinter')}",
                "width", "100%",
                "height", "100%",
                "align", "middle",
                "id", "Footprinter",
                "quality", "high",
                "bgcolor", "#C0C0C0",
                "name", "Footprinter",
                "allowScriptAccess", "always",
                "type", "application/x-shockwave-flash",
                "pluginspage", "http://www.adobe.com/go/getflashplayer",
                "flashvars", "xmin=${xmin}&ymin=${ymin}&xmax=${xmax}&ymax=${ymax}&servicePath=${mapURL}..........


On the Flex side it's:

<esri:ArcGISDynamicMapServiceLayer id="dynamicService" name="Map Legend"
                                                       url="{FlexGlobals.topLevelApplication.parameters.servicePath}"
                                                       load="dynamicService.defaultVisibleLayers()"
                                                       updateStart="showProgressBar()"
                                                       updateEnd="hideProgressBar()" >
                    <esri:alpha>
                        <mx:Number>.7</mx:Number>
                    </esri:alpha>
                    </esri:ArcGISDynamicMapServiceLayer>
0 Kudos
DanielMauer
New Contributor II
Wow, I didn't know I could just embed FlashVars values in non-script parts of the flex code with brackets like that.  This is *SO* much easier!  Many thanks!!
0 Kudos