Select to view content in your preferred language

Geocode a location and create a buffer of that location?

700
1
05-10-2011 08:28 AM
TristanForward2
Emerging Contributor
Hello,

I am trying to create a widget that will geolocate an address using bing, then create a buffer around from that geolocated location. I have used the FLEX API Library BufferSample.mxml, VEGEOCODER.mxml, to start development of the widget. I am having trouble passing a variable that contains the geometry of the point found from the geocoder to the buffer. Any suggestions on how to do this?

Also is there a problem with the geometry:Polygon in event.result. This part of the code used to run under a mouse_click event while now it's a button click event. Could I use the VEGeocoder.addressToLocationsComplete event somehow?

Thanks,

Tristan

EDIT: I was able to get it to work, now trying to fix the extent so that it is the extent of the buffer and not of the point.


<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:esri="http://www.esri.com/2008/ags"
               xmlns:s="library://ns.adobe.com/flex/spark"
               pageTitle="Bing Geocoding with ArcGIS API for Flex">
    <!--
         This sample shows how to find the address (aka geocoding) for a certain address.

         The solution is to use the addressToLocations() method on the VEGeocoder.

         In this sample a user-provided address is sent to the server
         for geocoding.  When the response comes back, the onResult()
         function creates a graphic for the result and then sets the
         map extent to the best view extent of the result.

         To use VETiledLayer/VEGeocoder you will need access to Bing Maps (formerly Microsoft Virtual Earth).
         See "Concepts" in the Resource Center for details.
         http://help.arcgis.com/en/webapi/flex/help/index.html#ve_gettingstarted.htm
    -->
 
 
 <!-- Resources
 
 http://resources.esri.com/help/9.3/arcgisserver/apis/flex/samples/index.html?sample=FreehandBufferGP
 http://resources.esri.com/help/9.3/arcgisserver/apis/flex/help/Content/ve_geocoding.htm
 http://forums.esri.com/Thread.asp?c=158&f=2421&t=281389
 http://help.arcgis.com/en/webapi/flex/samples/index.html?sample=LabelPoints
 -->
 
 <!-- Start Declarations -->
 <fx:Declarations>
  <!-- Replace this sample Bing key with your own Bing key, see http://help.arcgis.com/en/webapi/flex/help/index.html#ve_gettingstarted.htm -->
  <fx:String id="bingKey">AkzyGe3XdTPn1o1S0oJQT4c1rLDE_gjEOANKPmn0vAFYWZTHWa9L9ZmQ1picf5Sm</fx:String>
  
  <esri:VEGeocoder id="veGeocoder"
       culture="en-US"
       key="{bingKey}"/>
  
  <esri:SimpleMarkerSymbol id="mySymbol"
         alpha="0.5"
         color="0xFF0000"
         size="19"
         style="circle">
   <esri:SimpleLineSymbol width="2"/>
  </esri:SimpleMarkerSymbol>
  
  <esri:SimpleFillSymbol id="sfs" color="0xFF0000">
   <esri:SimpleLineSymbol color="0x000000"/>
  </esri:SimpleFillSymbol>
  
  <esri:GeometryService id="myGeometryService" url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
 </fx:Declarations>
 <!-- End Declarations -->
 
    <fx:Script>
        <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.GeometryServiceEvent;
   import com.esri.ags.events.VEGeocoderEvent;
   import com.esri.ags.geometry.Extent;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.geometry.Polygon;
   import com.esri.ags.tasks.supportClasses.BufferParameters;
   import com.esri.ags.utils.WebMercatorUtil;
   import com.esri.ags.virtualearth.VEGeocodeResult;
   
   import mx.controls.Alert;
   import mx.rpc.AsyncResponder;
   import mx.rpc.Fault;

   
   private function doFind():void
            {
                veGeocoder.addressToLocations(address.text, new AsyncResponder(onResult, onFault));
                function onResult(results:Array, token:Object = null):void
                {
                    if (results.length > 0)
                    {
                        var veResult:VEGeocodeResult = results[0];
                        var myGraphic:Graphic = new Graphic();  
      
                        myGraphic.geometry = WebMercatorUtil.geographicToWebMercator(veResult.location);
                        myGraphic.symbol = mySymbol;
                        myGraphic.toolTip = veResult.displayName;
                        myGraphic.id = "graphic";
                        myGraphicsLayer.add(myGraphic);
                        myMap.extent = WebMercatorUtil.geographicToWebMercator(veResult.bestView) as Extent;
      //myMap2.extent = WebMercatorUtil.geographicToWebMercator(veResult.location) as Extent;
                        myInfo.htmlText = "<b>Found:</b><br/>" + veResult.displayName;
      
      //Create the Buffer and set the Parameters
      var myMapPoint:MapPoint = MapPoint(myMap.extent.center);
 
      var bufferParameters:BufferParameters = new BufferParameters();
      
      // Note: As of version 2.0, the GeometryService input is geometries (instead of graphics).
      bufferParameters.geometries = [ myMapPoint ];
      
      //User input for Buffer Distances in km
      bufferParameters.distances = [ 50, 100 ];
      // Note: As of version 2.0, the buffer constants have been moved to GeometryService.
      bufferParameters.unit = GeometryService.UNIT_KILOMETER;
      
      //Spatial Reference for Web Mercator is (102113) Mercator Geographic is (4326)
      bufferParameters.bufferSpatialReference = new SpatialReference(102113);
      bufferParameters.outSpatialReference = myMap.spatialReference;
      //bufferParameters.outSpatialReference = new SpatialReference(102113);
 
      myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
      myGeometryService.buffer(bufferParameters);
      
      function bufferCompleteHandler(event:GeometryServiceEvent):void
      {
       myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
       // Note: As of version 2.0, GeometryService returns geometries (instead of graphics)
       for each (var geometry:Polygon in event.result)
       {
        var graphic2:Graphic = new Graphic();
        graphic2.geometry = geometry;
        //graphic2.geometry = WebMercatorUtil.geographicToWebMercator(veResult.location);
        graphic2.symbol = sfs;
        myGraphicsLayer.add(graphic2);
       }
      }
                    }
                    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);
                    }
                }
                function onFault(fault:Fault, token:Object = null):void
                {
                    myInfo.htmlText = "<b>Failure:</b> " + fault.faultString;
                    Alert.show("Failure: \n" + fault.faultString, "VEGeocoder problem");
                }
   }
        ]]>

    </fx:Script>



    <esri:Map id="myMap">
        <esri:extent>
            <esri:Extent xmin="-13046644" ymin="4028802" xmax="-13024380" ymax="4040893">
                <esri:SpatialReference wkid="102100"/>
            </esri:Extent>
        </esri:extent>
        <esri:VETiledLayer id="veTiledLayer"
                           culture="en-US"
                           key="{bingKey}"
                           loadError="Alert.show(event.fault.faultString, 'VETiledLayer loadError')"
                           mapStyle="road"/>
        <esri:GraphicsLayer id="myGraphicsLayer"/>
    </esri:Map>

    <s:Panel top="5"
             horizontalCenter="0"
             title="Find an address">
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <mx:Form>
            <mx:FormItem label="Address">
                <mx:TextInput id="address"
                              width="100%"
                              text="599 Brealey Dr, ON"/> <!-- Fleming College -->
            </mx:FormItem>
            <mx:FormItem>
                <mx:Button click="doFind()" label="Find Address"/>
            </mx:FormItem>
        </mx:Form>
        <mx:Text id="myInfo"
                 width="100%"
                 color="0x00FF00"
                 textAlign="center"/>
    </s:Panel>
</s:Application>

Tags (2)
0 Kudos
1 Reply
MLowry
by
Frequent Contributor
Ever get this working correctly? I need to do something similar.
0 Kudos