Select to view content in your preferred language

Geocode an address - Esri API sample

613
1
Jump to solution
02-20-2012 10:00 AM
philippschnetzer
Frequent Contributor
I am trying to get the 'Geocode an Address' sample from the Esri API samples page working.  I have modified the look of it a little and successfully brought it into the flexviewer.  Only problem is that I cannot get it top display the graphic on the map after its found a location.  It zooms to the location and displays the 'Found: 123 main street...' but no graphic.

Thanks for taking a look!:
<?xml version="1.0" encoding="utf-8"?> <viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"                xmlns:s="library://ns.adobe.com/flex/spark"                xmlns:mx="library://ns.adobe.com/flex/mx"       xmlns:viewer="com.esri.viewer.*"                xmlns:esri="http://www.esri.com/2008/ags">     <!--     In this sample, the addressToLocations() method on the Locator     is used to find the lcoation (aka geocoding) for a certain address.      The sample takes a user-provided address and sends it to the     server for geocoding.  When the response comes back, the OnResult()     function creates a graphic for the result and then sets the scale     appropriately based on if the match was a "street", "city" etc match.     -->      <fx:Script>         <![CDATA[             import com.esri.ags.Graphic;             import com.esri.ags.geometry.MapPoint;             import com.esri.ags.tasks.supportClasses.AddressCandidate;             import com.esri.ags.utils.WebMercatorUtil;     import com.esri.viewer.AppEvent;    import com.esri.ags.Map;               import mx.controls.Alert;             import mx.rpc.AsyncResponder;              private function doFind():void             {                 var myAddress:Object = { SingleLine: onelineaddress.text };                           // Use outFields to get back extra information                 // The exact fields available depends on the specific Locator used.                 var myOutFields:Array = [ "Loc_name" ];                 locateTask.outSpatialReference = map.spatialReference;                 locateTask.addressToLocations(myAddress, myOutFields, new AsyncResponder(onResult, onFault));                      function onResult(candidates:Array, token:Object = null):void                 {                     if (candidates.length > 0)                     {                         var addressCandidate:AddressCandidate = candidates[0];                         var myGraphic:Graphic = new Graphic();                          // for 9.3 servers, or anything else returning latlong:                //myGraphic.geometry = WebMercatorUtil.geographicToWebMercator(addressCandidate.location);                          myGraphic.geometry = addressCandidate.location;                         myGraphic.symbol = mySymbol;                         myGraphic.toolTip = addressCandidate.address.toString();                         myGraphic.id = "graphic";                         myGraphicsLayer.add(myGraphic);                          map.centerAt(myGraphic.geometry as MapPoint);                           // Zoom to an appropriate level                         // Note: your attribute and field value might differ depending on which Locator you are using...                         if (addressCandidate.attributes.Loc_name.search("RoofTop") > 0) // US_RoofTop                         {        map.scale = 10000;                         }                         else if (addressCandidate.attributes.Loc_name.search("Address") > 0)                         {        map.scale = 10000;                         }                         else if (addressCandidate.attributes.Loc_name.search("Street") > 0) // US_Streets, CAN_Streets, CAN_StreetName, EU_Street_Addr* or EU_Street_Name*                          {        map.scale = 15000;                         }                         else if (addressCandidate.attributes.Loc_name.search("ZIP4") > 0                             || addressCandidate.attributes.Loc_name.search("Postcode") > 0) // US_ZIP4, CAN_Postcode                         {        map.scale = 20000;                         }                         else if (addressCandidate.attributes.Loc_name.search("Zipcode") > 0) // US_Zipcode                         {        map.scale = 40000;                         }                         else if (addressCandidate.attributes.Loc_name.search("City") > 0) // US_CityState, CAN_CityProv                         {        map.scale = 150000;                         }                         else                         {        map.scale = 500000;                         }                         myInfo.htmlText = "Found:  </b>" + addressCandidate.address.toString(); // formated address                     }                     else                     {                         myInfo.htmlText = "<b><font color='#FFFFFF'>Found nothing, try again...</b></font>";                          Alert.show("Sorry, couldn't find a location for this address"                                    + "\nAddress: " + onelineaddress.text);                     };                  }                 function onFault(info:Object, token:Object = null):void                 {                     myInfo.htmlText = "<b>Failure</b>" + info.toString();                     Alert.show("Failure: \n" + info.toString());                 }                          }               ]]>     </fx:Script>      <fx:Declarations>       <esri:Locator id="locateTask" url="http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Address_NA_10/GeocodeServer"/>          <esri:SimpleMarkerSymbol id="mySymbol"                                  alpha="0.5"                                  color="0xFF0000"                                  size="21"                                  style="circle">             <esri:SimpleLineSymbol width="2"/>         </esri:SimpleMarkerSymbol>     </fx:Declarations>    <esri:Map>   <esri:extent> <esri:Extent>     <esri:SpatialReference wkid="102100"/>    </esri:Extent>    </esri:extent>      <esri:GraphicsLayer id="myGraphicsLayer"/>  </esri:Map>     <s:Group>        <s:layout>     <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle"/>    </s:layout>    <!-- Single line geocode -->    <s:TextInput id="onelineaddress"        width="100"        enter="doFind()"        text="Enter address..."/>    <s:Button click="doFind()" label="Find Address"/>    <mx:Text id="myInfo"       width="300"       color="0xFFFFFF"       />   </s:Group> </viewer:BaseWidget>
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Phillip,

   Widgets have access to the viewers map so you should not have :

    <esri:Map>         <esri:extent>             <esri:Extent>                 <esri:SpatialReference wkid="102100"/>             </esri:Extent>             </esri:extent>                  <esri:GraphicsLayer id="myGraphicsLayer"/>     </esri:Map>


In the MXML. You need to define the myGraphicsLayer in actionscript.

View solution in original post

0 Kudos
1 Reply
RobertScheitlin__GISP
MVP Emeritus
Phillip,

   Widgets have access to the viewers map so you should not have :

    <esri:Map>         <esri:extent>             <esri:Extent>                 <esri:SpatialReference wkid="102100"/>             </esri:Extent>             </esri:extent>                  <esri:GraphicsLayer id="myGraphicsLayer"/>     </esri:Map>


In the MXML. You need to define the myGraphicsLayer in actionscript.
0 Kudos