![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?
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/mx" minWidth ="955" minHeight ="600" xmlns:esri ="http://www.esri.com/2008/ags" >
< fx:Script >
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.GeometryServiceEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.tasks.supportClasses.BufferParameters;
import com.esri.ags.tasks.supportClasses.IdentifyParameters;
import com.esri.ags.tasks.supportClasses.IdentifyResult;
import com.esri.ags.utils.GeometryUtil;
import mx.events.ItemClickEvent;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import org.osmf.events.BufferEvent;
//设置ToggleButtonBar的itemClickhandler事件,用event.index属性获得目标button的序号
private function itemClickHandler(event:ItemClickEvent):void
{
switch(event.index)
{
case 0:
{
drawTool.activate(com.esri.ags.tools.DrawTool.MAPPOINT);
break;
}
case 1:
{
drawTool.activate(com.esri.ags.tools.DrawTool.POLYLINE);
break;
}
case 2:
{
drawTool.activate(com.esri.ags.tools.DrawTool.FREEHAND_POLYLINE);
break;
}
case 3:
{
drawTool.activate(com.esri.ags.tools.DrawTool.POLYGON);
break;
}
case 4:
{
drawTool.activate(com.esri.ags.tools.DrawTool.FREEHAND_POLYGON);
break;
}
}
}
//定义响应绘制完毕的ondrawEnd函数,注意参数是event:DrawEvent,首先把DrawTool绘制的graphic添加到GraphicsLayer中以便显示出来
//2 判断graphic的几何属性,以便于区别的缓冲分析,通过定义了bufferGeometry函数,参数是graphic的geometry属性
private function ondrawEnd(event:DrawEvent):void
{
var graphic:Graphic=event.graphic;
myGraphicsLayer.add(graphic);
switch (graphic.geometry.type)
{
case Geometry.MAPPOINT:
{
bufferGeometry(graphic.geometry);
break;
}
//下面这段笔者认为是把线拆成点数组,再进行缓冲分析(但实验这里没成功败笔啊)
case Geometry.POLYLINE:
{
GeometryUtil.normalizeCentralMeridian([graphic.geometry],myGeometryService,new AsyncResponder(getLine,faultHandler));
function getLine(item:Object,token:Object=null):void
{
bufferGeometry((item as Array)[0] as Geometry);
}
function faultHandler(fault:Fault,asyncToken:AsyncToken):void
{
}
break;
}
case Geometry.POLYGON:
{
var geometryPolygon:Polygon = graphic.geometry as Polygon;
myMap.centerAt(geometryPolygon.extent.center);
break;
}
}
//设置bufferGeometry函数,在进行缓冲分析要设置一个参数组bufferParameters进行传递,具体属性有geometries,distances,unit
//outSpatialReference,这里需要注意的是,在API旧版本中没有geometries属性(用来获得绘制图形的几何属性),而是一个
//features属性。
//myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);为myGeometryService添加
//一个事件侦听器,一旦myGeometryService.buffer(bufferParameters)事件结束,立即响应bufferCompleteHandler1函数
function bufferGeometry(geometry:Geometry):void
{
var bufferParameters:BufferParameters = new BufferParameters();
bufferParameters.geometries = [geometry];
bufferParameters.distances = [ Number(bText.text)];
bufferParameters.unit = GeometryService.UNIT_METER;
bufferParameters.outSpatialReference = myMap.spatialReference;
myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);
myGeometryService.buffer(bufferParameters);
}
//设置bufferCompleteHandler1函数,参数是event:GeometryServiceEvent,该参数拥有一个result属性,可以用遍历它获得返回的
//结果(这里应该是Polygon,因为result类型设置为Object),把返回的结果添加到GraphicsLayer中显示
//接下来定义了一个identityParams参数组用来传递,具体参数有returnGeometry,tolerance等,其中有一个参数需要注意
//layerIds是图层的序号(实验是获得State图层,所以识别也是在这一图层)最后就是调用execute了identifyTask.execute(identifyParams);
function bufferCompleteHandler1(event:GeometryServiceEvent):void
{
for each (var polygon:Polygon in event.result)
{
var graphic:Graphic = new Graphic(polygon);
graphic.symbol = sfs;
myGraphicsLayer.add(graphic);
var identifyParams:IdentifyParameters=new IdentifyParameters();
identifyParams.returnGeometry=true;
identifyParams.tolerance=1;
identifyParams.width = myMap.width;
identifyParams.height = myMap.height;
identifyParams.geometry=graphic.geometry;
identifyParams.layerIds=[2];
identifyParams.mapExtent=myMap.extent;
identifyTask.execute(identifyParams);
}
myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);
}
}
//设置IdentitifyTask识别结束后响应函数identifyCompleteHandler,参数是event:IdentifyEvent,该参数拥有一个identifyResults
//属性,也就是返回结果,另外设置了一个data数组获取返回结果的值,最终赋予表格resultTable.
private function identifyCompleteHandler(event:IdentifyEvent):void
{
var datas:Array=new Array();
for each(var result:IdentifyResult in event.identifyResults)
{
myGraphicsLayer.add(result.feature);
datas.push({"value":result.value});
}
resultTable.dataProvider=datas;
}
]]>
</ fx:Script >
< fx:Declarations >
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
//设置绘制的符号化
< esri:SimpleLineSymbol id ="sls"
width ="1"
alpha ="1"
color ="0xFF0000"
style ="dash" />
<!-- Symbol for Point -->
< esri:SimpleMarkerSymbol id ="sms"
color ="0xFF0000"
size ="10"
style ="square" >
< esri:SimpleLineSymbol width ="2" color ="0x00FF00" />
</ esri:SimpleMarkerSymbol >
<!-- Symbol for Polygon -->
< esri:SimpleFillSymbol id ="sfs" alpha ="0.8" >
< esri:SimpleLineSymbol width ="4"
alpha ="1"
color ="#FF0000"
style ="solid" />
</ esri:SimpleFillSymbol >
//定义了一个绘图工具,绘制完毕响应ondrawEnd函数
< esri:DrawTool id ="drawTool"
drawEnd ="ondrawEnd(event)"
fillSymbol ="{sfs}"
lineSymbol ="{sls}"
map ="{myMap}"
markerSymbol ="{sms}" /> //下面设置了一个几何服务,本例子主要用来进行缓冲分析
< esri:GeometryService id ="myGeometryService" url ="http://shibaizhong-pc/ArcGIS/rest/services/Geometry/GeometryServer" />
< esri:IdentifyTask id ="identifyTask" executeComplete ="identifyCompleteHandler(event)"
url ="http://shibaizhong-pc/ArcGIS/rest/services/USA/MapServer" />
< esri:Extent id ="allUsa" xmin ="-183.780014745329" ymin ="16.2975638854873" xmax ="-61.4068547410964" ymax ="74.0304580085983" />
</ fx:Declarations >
< s:layout >
< s:HorizontalLayout />
</ s:layout >
< s:BorderContainer height ="100%" width ="80%" borderStyle ="solid" borderWeight ="2" borderColor ="#439AD7" >
< s:layout >
< s:VerticalLayout paddingLeft ="10" paddingTop ="10" >
</ s:VerticalLayout >
</ s:layout >
< mx:ToggleButtonBar id ="tbb"
creationComplete ="tbb.selectedIndex = -1"
itemClick ="itemClickHandler(event)"
toggleOnClick ="true"
valueCommit ="if (tbb.selectedIndex == -1) { drawTool.deactivate() }" >
< mx:dataProvider >
< fx:Object id ="p0" icon ="@Embed(source='/assets/measure-point.png')" />
< fx:Object id ="p1" icon ="@Embed(source='/assets/measure-line.png')" />
< fx:Object id ="p2"
icon ="@Embed(source='/assets/measure-line.png')"
label ="FreeHand" />
< fx:Object id ="p3" icon ="@Embed(source='/assets/measure-poly.png')" />
< fx:Object id ="p4"
icon ="@Embed(source='/assets/measure-poly.png')"
label ="FreeHand" />
</ mx:dataProvider >
</ mx:ToggleButtonBar >
< mx:ControlBar width ="100%" >
< s:Label text ="缓冲距离 (点线):" />
< mx:TextInput id ="bText" //用来获取缓冲分析的距离;
width ="45"
maxChars ="5"
text ="100000"
textAlign ="right" />
< s:Label text =" meters." />
</ mx:ControlBar >
< esri:Map id ="myMap" logoVisible ="false" panArrowsVisible ="true" extent ="{allUsa}" >
< esri:ArcGISDynamicMapServiceLayer url ="http://shibaizhong-pc/ArcGIS/rest/services/USA/MapServer" />
< esri:GraphicsLayer id ="myGraphicsLayer" />
</ esri:Map >
</ s:BorderContainer >
< mx:DataGrid id ="resultTable" height ="100%" width ="17%" borderStyle ="solid" borderColor ="#D6D7D8" />
</ s:Application >
< 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/mx" minWidth ="955" minHeight ="600" xmlns:esri ="http://www.esri.com/2008/ags" >
< fx:Script >
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.GeometryServiceEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.tasks.supportClasses.BufferParameters;
import com.esri.ags.tasks.supportClasses.IdentifyParameters;
import com.esri.ags.tasks.supportClasses.IdentifyResult;
import com.esri.ags.utils.GeometryUtil;
import mx.events.ItemClickEvent;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import org.osmf.events.BufferEvent;
//设置ToggleButtonBar的itemClickhandler事件,用event.index属性获得目标button的序号
private function itemClickHandler(event:ItemClickEvent):void
{
switch(event.index)
{
case 0:
{
drawTool.activate(com.esri.ags.tools.DrawTool.MAPPOINT);
break;
}
case 1:
{
drawTool.activate(com.esri.ags.tools.DrawTool.POLYLINE);
break;
}
case 2:
{
drawTool.activate(com.esri.ags.tools.DrawTool.FREEHAND_POLYLINE);
break;
}
case 3:
{
drawTool.activate(com.esri.ags.tools.DrawTool.POLYGON);
break;
}
case 4:
{
drawTool.activate(com.esri.ags.tools.DrawTool.FREEHAND_POLYGON);
break;
}
}
}
//定义响应绘制完毕的ondrawEnd函数,注意参数是event:DrawEvent,首先把DrawTool绘制的graphic添加到GraphicsLayer中以便显示出来
//2 判断graphic的几何属性,以便于区别的缓冲分析,通过定义了bufferGeometry函数,参数是graphic的geometry属性
private function ondrawEnd(event:DrawEvent):void
{
var graphic:Graphic=event.graphic;
myGraphicsLayer.add(graphic);
switch (graphic.geometry.type)
{
case Geometry.MAPPOINT:
{
bufferGeometry(graphic.geometry);
break;
}
//下面这段笔者认为是把线拆成点数组,再进行缓冲分析(但实验这里没成功败笔啊)
case Geometry.POLYLINE:
{
GeometryUtil.normalizeCentralMeridian([graphic.geometry],myGeometryService,new AsyncResponder(getLine,faultHandler));
function getLine(item:Object,token:Object=null):void
{
bufferGeometry((item as Array)[0] as Geometry);
}
function faultHandler(fault:Fault,asyncToken:AsyncToken):void
{
}
break;
}
case Geometry.POLYGON:
{
var geometryPolygon:Polygon = graphic.geometry as Polygon;
myMap.centerAt(geometryPolygon.extent.center);
break;
}
}
//设置bufferGeometry函数,在进行缓冲分析要设置一个参数组bufferParameters进行传递,具体属性有geometries,distances,unit
//outSpatialReference,这里需要注意的是,在API旧版本中没有geometries属性(用来获得绘制图形的几何属性),而是一个
//features属性。
//myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);为myGeometryService添加
//一个事件侦听器,一旦myGeometryService.buffer(bufferParameters)事件结束,立即响应bufferCompleteHandler1函数
function bufferGeometry(geometry:Geometry):void
{
var bufferParameters:BufferParameters = new BufferParameters();
bufferParameters.geometries = [geometry];
bufferParameters.distances = [ Number(bText.text)];
bufferParameters.unit = GeometryService.UNIT_METER;
bufferParameters.outSpatialReference = myMap.spatialReference;
myGeometryService.addEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);
myGeometryService.buffer(bufferParameters);
}
//设置bufferCompleteHandler1函数,参数是event:GeometryServiceEvent,该参数拥有一个result属性,可以用遍历它获得返回的
//结果(这里应该是Polygon,因为result类型设置为Object),把返回的结果添加到GraphicsLayer中显示
//接下来定义了一个identityParams参数组用来传递,具体参数有returnGeometry,tolerance等,其中有一个参数需要注意
//layerIds是图层的序号(实验是获得State图层,所以识别也是在这一图层)最后就是调用execute了identifyTask.execute(identifyParams);
function bufferCompleteHandler1(event:GeometryServiceEvent):void
{
for each (var polygon:Polygon in event.result)
{
var graphic:Graphic = new Graphic(polygon);
graphic.symbol = sfs;
myGraphicsLayer.add(graphic);
var identifyParams:IdentifyParameters=new IdentifyParameters();
identifyParams.returnGeometry=true;
identifyParams.tolerance=1;
identifyParams.width = myMap.width;
identifyParams.height = myMap.height;
identifyParams.geometry=graphic.geometry;
identifyParams.layerIds=[2];
identifyParams.mapExtent=myMap.extent;
identifyTask.execute(identifyParams);
}
myGeometryService.removeEventListener(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler1);
}
}
//设置IdentitifyTask识别结束后响应函数identifyCompleteHandler,参数是event:IdentifyEvent,该参数拥有一个identifyResults
//属性,也就是返回结果,另外设置了一个data数组获取返回结果的值,最终赋予表格resultTable.
private function identifyCompleteHandler(event:IdentifyEvent):void
{
var datas:Array=new Array();
for each(var result:IdentifyResult in event.identifyResults)
{
myGraphicsLayer.add(result.feature);
datas.push({"value":result.value});
}
resultTable.dataProvider=datas;
}
]]>
</ fx:Script >
< fx:Declarations >
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
//设置绘制的符号化
< esri:SimpleLineSymbol id ="sls"
width ="1"
alpha ="1"
color ="0xFF0000"
style ="dash" />
<!-- Symbol for Point -->
< esri:SimpleMarkerSymbol id ="sms"
color ="0xFF0000"
size ="10"
style ="square" >
< esri:SimpleLineSymbol width ="2" color ="0x00FF00" />
</ esri:SimpleMarkerSymbol >
<!-- Symbol for Polygon -->
< esri:SimpleFillSymbol id ="sfs" alpha ="0.8" >
< esri:SimpleLineSymbol width ="4"
alpha ="1"
color ="#FF0000"
style ="solid" />
</ esri:SimpleFillSymbol >
//定义了一个绘图工具,绘制完毕响应ondrawEnd函数
< esri:DrawTool id ="drawTool"
drawEnd ="ondrawEnd(event)"
fillSymbol ="{sfs}"
lineSymbol ="{sls}"
map ="{myMap}"
markerSymbol ="{sms}" /> //下面设置了一个几何服务,本例子主要用来进行缓冲分析
< esri:GeometryService id ="myGeometryService" url ="http://shibaizhong-pc/ArcGIS/rest/services/Geometry/GeometryServer" />
< esri:IdentifyTask id ="identifyTask" executeComplete ="identifyCompleteHandler(event)"
url ="http://shibaizhong-pc/ArcGIS/rest/services/USA/MapServer" />
< esri:Extent id ="allUsa" xmin ="-183.780014745329" ymin ="16.2975638854873" xmax ="-61.4068547410964" ymax ="74.0304580085983" />
</ fx:Declarations >
< s:layout >
< s:HorizontalLayout />
</ s:layout >
< s:BorderContainer height ="100%" width ="80%" borderStyle ="solid" borderWeight ="2" borderColor ="#439AD7" >
< s:layout >
< s:VerticalLayout paddingLeft ="10" paddingTop ="10" >
</ s:VerticalLayout >
</ s:layout >
< mx:ToggleButtonBar id ="tbb"
creationComplete ="tbb.selectedIndex = -1"
itemClick ="itemClickHandler(event)"
toggleOnClick ="true"
valueCommit ="if (tbb.selectedIndex == -1) { drawTool.deactivate() }" >
< mx:dataProvider >
< fx:Object id ="p0" icon ="@Embed(source='/assets/measure-point.png')" />
< fx:Object id ="p1" icon ="@Embed(source='/assets/measure-line.png')" />
< fx:Object id ="p2"
icon ="@Embed(source='/assets/measure-line.png')"
label ="FreeHand" />
< fx:Object id ="p3" icon ="@Embed(source='/assets/measure-poly.png')" />
< fx:Object id ="p4"
icon ="@Embed(source='/assets/measure-poly.png')"
label ="FreeHand" />
</ mx:dataProvider >
</ mx:ToggleButtonBar >
< mx:ControlBar width ="100%" >
< s:Label text ="缓冲距离 (点线):" />
< mx:TextInput id ="bText" //用来获取缓冲分析的距离;
width ="45"
maxChars ="5"
text ="100000"
textAlign ="right" />
< s:Label text =" meters." />
</ mx:ControlBar >
< esri:Map id ="myMap" logoVisible ="false" panArrowsVisible ="true" extent ="{allUsa}" >
< esri:ArcGISDynamicMapServiceLayer url ="http://shibaizhong-pc/ArcGIS/rest/services/USA/MapServer" />
< esri:GraphicsLayer id ="myGraphicsLayer" />
</ esri:Map >
</ s:BorderContainer >
< mx:DataGrid id ="resultTable" height ="100%" width ="17%" borderStyle ="solid" borderColor ="#D6D7D8" />
</ s:Application >