Select to view content in your preferred language

Reprojecting Address Locator in Flex API

457
2
07-25-2011 09:02 AM
JenniferGrew1
Emerging Contributor
I am new to ArcGIS API for Flex and want to use the address locator in a map using locally projected data layers. Everything is local and rscheitlin posts for this very issue in the Flex Viewer http://forums.arcgis.com/threads/2666-Locator-in-sample-working-but-still-using-wrong-coordinate-sys... were very useful, but i can't seem to translate it over to the API. I apologize for the layout of the code below, it is scattered in several places throughout the mxml.

Here is what we have so far:

// Look up for Address
   private function doFind():void
   {
    var myAddress:Object =
     {
      Address: address.text,
       City: city.text,
       State: state.text,
       Zip: zip.text,
       /* Country: country.selectedItem */
       Country: "USA"
     };
    // Use outFields to get back extra information
    // The exact fields available depends on the specific Locator used.
    var myOutFields:Array = [ "Loc_name" ];
    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();
      //trace(addressCandidate.location);
      myGraphic.geometry = WebMercatorUtil.geographicToWebMercator(addressCandidate.location);
     
      //trace(myGraphic.geometry);
      myGraphic.symbol = mySymbol;
      myGraphic.toolTip = addressCandidate.address.toString();
      myGraphic.id = "graphic";
      geocodeLayer.add(myGraphic);
      mainMap.centerAt(myGraphic.geometry as MapPoint);
     
     
      // Zoom to an appropriate level
      // Note: your tile levels and LOC_NAME might differ...
      if (addressCandidate.attributes.Loc_name.search("RoofTop") > 0)
      {
       mainMap.scale = 10000;
      }
      else if (addressCandidate.attributes.Loc_name.search("Address") > 0)
      {
       mainMap.scale = 10000;
      }
      else if (addressCandidate.attributes.Loc_name.search("Street") > 0)
      {
       mainMap.scale = 15000;
      }
      else if (addressCandidate.attributes.Loc_name.search("Zipcode") > 0)
      {
       mainMap.scale = 40000;
      }
      else if (addressCandidate.attributes.Loc_name.search("City") > 0)
      {
       mainMap.scale = 150000;
      }
      else
      {
       mainMap.scale = 500000;
      }
      //myInfo.htmlText = "<b>Found:</b><br/>" + addressCandidate.address.toString(); // formated address
     }
     else
     {
      //myInfo.htmlText = "<b><font color='#FF0000'>Found nothing :(</b></font>";
      Alert.show("Sorry, couldn't find a location for this address"
       + "\nAddress: " + address.text
       + "\nCity: " + city.text
       + "\nZIP Code: " + zip.text
       + "\nState: " + state.text
       /* + "\nCountry: " + country.selectedItem) */
       + "\nCountry: USA");
     }
    }
<fx: Declarations>
  <esri:Locator id="locateTask" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer"/>
   <esri:SimpleMarkerSymbol id="mySymbol"
         alpha="0.5"
         color="0xFF0000"
         size="19"
         style="circle">
   <esri:SimpleLineSymbol width="2"/>
  </esri:SimpleMarkerSymbol>

<mx:Form id="AddressSearch" label="Address Search">
       <mx:FormItem label="Street">
        <s:TextInput width="100%" id="address" text="850 A Street"/>
       </mx:FormItem>
       <mx:FormItem label="City">
        <s:TextInput width="100%" id="city" text="Plummer"/>
       </mx:FormItem>
       <mx:FormItem label="Zip Code or postal code">
        <s:TextInput width="100" id="zip" text="83851"/>
       </mx:FormItem>
       <mx:FormItem label="State/Province">
        <s:TextInput width="100" id="state" text="ID"/>
       </mx:FormItem>
       <mx:FormItem>
        <s:Button id="geocodeButton" label="Find Address"
            click="doFind()"/>           
       </mx:FormItem>           
      </mx:Form>
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Jennifer,

  You need to set the outSpatialReference of the locator task.

<esri:Locator id="locateTask"  url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer" outSpatialReference="{mainMap.spatialReference}"/>


and then change this line:

myGraphic.geometry =addressCandidate.location;
0 Kudos
JenniferGrew1
Emerging Contributor
That worked perfectly! Thank you so much!
0 Kudos