Using a URL parameter to filter data on a Flex Map

2763
6
06-04-2012 05:47 AM
JohnPeabody
New Contributor II
I am a relative novice to Flex using Flex Viewer 2.5 and trying to find out if it is possible to pass a parameter via the URl and then use this parameter to filter the features displayed on the map for one or more layers on the map.

If it is possible what I would need to do to implement this functionality.

Thanking you in advance

John
Tags (2)
0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor
Have you seen this sample that shows the syntax for passing parameters through the URL?
0 Kudos
JohnPeabody
New Contributor II
Have you seen this sample that shows the syntax for passing parameters through the URL?


Thanks for the reply - I have used URL parameters to navigate around the map (as in the sample) - I'm trying to find out if you can use a URL parameter to filter the features shown in a layer on the map. Or to put it another way to manipulate the data shown on the map rather than the map itself

Thanks again

John
0 Kudos
KenBuja
MVP Esteemed Contributor
I've modified this sample to pass in a parameter in the URL. To use it, your URL would look like http://yoursite/testing/URLTest.html?id=Illinois

Without the parameter(?id=Illinois), it defaults to California.

<?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:esri="http://www.esri.com/2008/ags"
               pageTitle="FeatureLayer and its definitionExpression">
    
    <!--
    Problem:
    How to search a layer for certain features and display the results on a map?
    
    Solution:
    The easiest way is to use "FeatureLayer" and modifying the definitionExpression property
    based on the user input.
    
    This sample use FeatureLayer and simply updates its definitionExpression when
    user clicks the "Search" button (or hits enter in the text box), the definitionExpression
    is then updated on the FeatureLayer which will request its new features.
    
    Note that the busy cursor is shown while the definitionExpression is being updated.
    
    Tip 1: if you wanted to use a different symbol, specify a symbol or renderer on the feature layer.
    Tip 2: if you want to add a toolTip, listen to the graphicAdd event and add the tooltip at that time.
    -->
    
    <s:layout>
        <s:VerticalLayout gap="10"
                          horizontalAlign="center"
                          paddingBottom="20"
                          paddingLeft="20"
                          paddingRight="20"
                          paddingTop="20"/>
    </s:layout>
    
    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.LayerEvent;
            
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            
            private function doSearch():void
            {
                // fLayer.layerDetails.displayField
                fLayer.definitionExpression = "STATE_NAME like '" + qText.text + "'";
            }
            
            // the following four functions are 'just' error handling and showing/hiding the busy cursor
            protected function fLayer_updateStartHandler(event:LayerEvent):void
            {
                this.cursorManager.setBusyCursor();
            }
            
            protected function fLayer_updateEndHandler(event:LayerEvent):void
            {
                if (event.fault)
                {
                    trace("updateEnd: " + event.fault); // maybe a badly formatted query?
                }
                else if (event.updateSuccess == false)
                {
                    trace(event.type + ": " + event.updateSuccess + " ... unexpected failure");
                }
                else // things seem OK
                {
                    if (FeatureLayer(event.layer).numGraphics < 1)
                    {
                        Alert.show("Sorry, found no such features, please try something else");
                    }
                }
                this.cursorManager.removeBusyCursor();
            }
            
            protected function fLayer_faultHandler(event:FaultEvent):void
            {
                Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "FeatureLayer Fault " + event.fault.faultCode);
            }
            
            protected function fLayer_loadErrorHandler(event:LayerEvent):void
            {
                Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "FeatureLayer Load Error " + event.fault.faultCode);
            }
            
            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;
            }
            
            private function loading_Event():void
            {
                var params:Object = getURLParameters();
                if (params["id"])
                    {
                        qText.text = params.id;
                    }
                else
                {
                    qText.text = "California";
                }
                
                doSearch();
            }
            
        ]]>
    </fx:Script>
    
    <s:Panel height="60"
             backgroundColor="0xB2BFC6"
             title="Query a layer (search for U.S. states)">
        <s:layout>
            <s:HorizontalLayout/>
        </s:layout>
        <s:TextInput id="qText"
                     width="100%"
                     enter="doSearch()"
                     text="California"
                     toolTip="You may use % as a wildcard, e.g., New%"/>
        <s:Button click="doSearch()" label="Search"/>
    </s:Panel>
    
    <esri:Map id="myMap">
        <esri:extent>
            <esri:Extent xmin="-14305000" ymin="2748000" xmax="-6815000" ymax="7117000">
                <esri:SpatialReference wkid="102100"/>
            </esri:Extent>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Physical_Map/MapServer"/>
        <esri:FeatureLayer id="fLayer"
                           fault="fLayer_faultHandler(event)"
                           load="{loading_Event()}"
                           loadError="fLayer_loadErrorHandler(event)"
                           mode="snapshot"
                           updateEnd="fLayer_updateEndHandler(event)"
                           updateStart="fLayer_updateStartHandler(event)"
                           url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
    </esri:Map>
    
</s:Application>
0 Kudos
AnthonyGiles
Frequent Contributor
John,

Roberts esearch widget allows you to configure a URL to perform a search

http://www.arcgis.com/home/item.html?id=5d4995ccdb99429185dfd8d8fb2a513e

May be what you are looking for

Regards

Anthony
0 Kudos
KenBuja
MVP Esteemed Contributor
Sorry, I glossed over the part about this being a Flex Viewer problem. Please note that there's a forum dedicated to the Flex Viewer and questions related to that should go there.
0 Kudos
JohnPeabody
New Contributor II
Wasn't aware there was a forum dedicated to Flex Viewer - will repost there

Thanks everyone for the input
0 Kudos