Select to view content in your preferred language

Buffer Ring

1683
2
02-24-2011 11:38 AM
KarenDenny
New Contributor
I'm trying to get a buffer ring to draw around a point on a map.  Ideally user will click on map and buffer be drawn around that point (multi-rings).  I've tried several different combinations of things and can't seem to get it to work.  I can get the points on the map to show up when clicked, but don't know how to get the buffers around that clicked point. 

<?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:mx="library://ns.adobe.com/flex/halo"
xmlns:esri1="http://www.esri.com/2008/ags" xmlns:mx1="library://ns.adobe.com/flex/mx"
>

  <fx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.GeometryServiceEvent;
   import com.esri.ags.events.GeoprocessorEvent;
   import com.esri.ags.events.MapMouseEvent;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.layers.TiledMapServiceLayer;
   import com.esri.ags.symbols.SimpleFillSymbol;
   import com.esri.ags.symbols.SimpleLineSymbol;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   import com.esri.ags.tasks.supportClasses.BufferParameters;
   import com.esri.ags.utils.WebMercatorUtil;
  
   import mx.events.FlexEvent;
  
   private function layerShowHandler(event:FlexEvent):void
   {
    // update the LODs/zoomslider to use/show the levels for the selected base map
    var tiledLayer:TiledMapServiceLayer = event.target as TiledMapServiceLayer;
    myMap.lods = tiledLayer.tileInfo.lods;
   }
  
   private function onMapClick(event:MapMouseEvent):void
   {
    var mapPoint:MapPoint = event.mapPoint;
    mapPoint.spatialReference = myMap.spatialReference;
    var graphic:Graphic = new Graphic(mapPoint);
    graphic.symbol = new SimpleMarkerSymbol("cross",10,0,1,0,0,0,
     new SimpleLineSymbol("solid",0,1,2));
    graphicsLayer.add(graphic);
   }
  
   private function bufferMapPoint():void
   {

    var bufferParameters:BufferParameters = new BufferParameters();
    bufferParameters.distances = [5,10];
    bufferParameters.unit = GeometryService.UNIT_STATUTE_MILE;
    bufferParameters.bufferSpatialReference = myMap.spatialReference;
   
    myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE,bufferCompleteHandler);
    myGeometryService.buffer(bufferParameters);
   
    function
     bufferCompleteHandler(event:GeometryServiceEvent):void
    {
     myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE,bufferCompleteHandler);
      for each (var graphic:Graphic in event)
     {
      graphic.symbol = sfs;
      graphicsLayer.add(graphic);
     }
    }
   }
  
  ]]>
</fx:Script>
<esri1:Map id="myMap"
      level="4"
      load="myMap.centerAt(new MapPoint(-11713000, 4822000))"
      mapClick="onMapClick(event)">
  <esri1:ArcGISTiledMapServiceLayer show="layerShowHandler(event)"
            url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"
            visible="{bb.selectedIndex == 0}"/>
  <esri1:ArcGISTiledMapServiceLayer show="layerShowHandler(event)"
            url="http://server.arcgisonline.com/ArcGIS/rest/services/USA_Topo_Maps/MapServer"
            visible="{bb.selectedIndex == 1}"/>
  <esri1:ArcGISTiledMapServiceLayer show="layerShowHandler(event)"
            url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
            visible="{bb.selectedIndex == 2}"/>
  <esri1:GraphicsLayer id="graphicsLayer"/>
</esri1:Map>

<s:ButtonBar id="bb"
     right="5" top="5"
     requireSelection="true">
  <s:dataProvider>
   <s:ArrayList>
    <fx:String>Streets</fx:String>
    <fx:String>U.S. Topo</fx:String>
    <fx:String>Imagery</fx:String>
   </s:ArrayList>
  </s:dataProvider>
</s:ButtonBar>


<fx:Style>
  @namespace s "library://ns.adobe.com/flex/spark";
  @namespace mx "library://ns.adobe.com/flex/mx";
  @namespace esri "http://www.esri.com/2008/ags";
 
  esri|Navigation
  {
   skinClass: ClassReference("com.esri.ags.samples.skins.MyNavigationSkin");
   left: 20;
   top: 20;
  }
</fx:Style>

  
<fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
  <esri1:SimpleFillSymbol id="sfs" color="0xFF0000">
   <esri1:SimpleLineSymbol color="0x000000"/>
  </esri1:SimpleFillSymbol>
  <esri1:GeometryService id="myGeometryService"
   url="http://sampleserver2.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
</fx:Declarations>

<s:controlBarLayout>
  <s:HorizontalLayout horizontalAlign="center" paddingBottom="7" paddingTop="7"/>
</s:controlBarLayout>

<s:controlBarContent>
  <s:Button click="bufferMapPoint()" label="Buffer Map Point"/>
</s:controlBarContent>
 
</s:Application>
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Karen,

   Here is your the part of your code that had issues with some comments:

   private function bufferMapPoint():void
   {
    
    var bufferParameters:BufferParameters = new BufferParameters();
    bufferParameters.distances = [5,10];
    //You were never passing in any geometry to the buffer parameters
    bufferParameters.geometries = [graphicsLayer.graphicProvider[0].geometry]
    bufferParameters.unit = GeometryService.UNIT_STATUTE_MILE;
    bufferParameters.bufferSpatialReference = myMap.spatialReference;
    
    myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE,bufferCompleteHandler);
    myGeometryService.buffer(bufferParameters);
    
    function bufferCompleteHandler(event:GeometryServiceEvent): void
    {
     myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE,bufferCompleteHandler);
     //The result of a buffer operation does not return graphics in the 2.x API
     //it returns geometries
     for each (var geom:Geometry in event.result)
     {
      var graphic:Graphic = new Graphic();
      graphic.symbol = sfs;
      graphic.geometry = geom;
      graphicsLayer.add(graphic);
     }
    }
   }
0 Kudos
KarenDenny
New Contributor
Many thanks for the help!
0 Kudos