arcgis api for flex 开发入门(七)Geometry service 的使用
Geometry service 顾名思义,就是提供针对几何层级的服务,比如说Project,
Simplify , Buffer,Areas And Lengths , Lengths 等
详细信息可以到下面的地址去看一下
http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/Geo
metryServer
这个网址发布了上面5个Geometry service。
下面我们就来讲解如何在你的flex 地图中使用Geometry service,我们拿最常用
buffer来做例子。
首先 定义一个GeometryService
<esri:GeometryService id="myGeometryService"
url="http://sampleserver2.arcgisonline.com/ArcGIS/rest/services/Geometr
y/GeometryServer"/>
id 唯一标识了这个GeometryService,url指定了GeometryService的地址
和identify工具类似,要进行buffer操作我们先创建一个需要做buffer的几何,
然后定义一个BufferParameters ,然后执行buffer操作,最后将buffer的结果绘
制到GraphicsLayer上。
下面我们介绍一下BufferParameters参数的意义distances为buffer半径,
features 为需要做buffer的要素集合,unit为单位bufferSpatialReference为
buffer操作时的空间参照系。
设置完参数后,要添加buffer完成时间的EventListener,当buffer完成后我们就
可以处理这个buffer的结果了,如下面AS3脚本
var bufferParameters : BufferParameters = new BufferParameters();
bufferParameters.features = [point];
bufferParameters.distances = [3000];
bufferParameters.unit = BufferParameters.UNIT_METER;
bufferParameters.bufferSpatialReference = new SpatialReference(
02113); myGeometryService.addEventListener
(GeometryServiceEvent.BUFFER_COMPLETE, bufferCompleteHandler);
myGeometryService.buffer( bufferParameters );
关键的一点,bufferParameters.features参数设置的features 一定要有空间参
考系,不然buffer不成功。
其余的绘制操作,将结果绘制到GraphicsLayer上的操作和上一讲基本相同,就不
再讲了。
完整代码:
Code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:esri="http://www.esri.com/2008/ags"
layout="absolute"
>
<mx:Script>
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.symbol.Symbol;
import com.esri.ags.toolbars.Draw;
import com.esri.ags.SpatialReference;
import com.esri.ags.events.GeometryServiceEvent;
import com.esri.ags.tasks.BufferParameters;
private function drawEndHandler(eventrawEvent):void
{
var geometry : Geometry = event.geometry;
var myMapPoint:Graphic = new Graphic();
myMapPoint.geometry = geometry;
geometry.spatialReference = new
SpatialReference(4016);
var bufferParameters : BufferParameters = new
BufferParameters();
bufferParameters.features = [myMapPoint];
bufferParameters.distances = [300000];
bufferParameters.unit = BufferParameters.UNIT_METER;
bufferParameters.bufferSpatialReference = new
SpatialReference(102113);
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.graphics
)
{
graphic.symbol = sfs;
myGraphicsLayer.add( graphic );
}
}
]]>
</mx:Script>
<esri:SimpleFillSymbol id="sfs" color="0xFF0000">
<esri:SimpleLineSymbol color="0x000000"/>
</esri:SimpleFillSymbol>
<!-- Draw ToolBar -->
<esriraw id="drawToolbar" map="{map}"
graphicsLayer="{myGraphicsLayer}" drawEnd="drawEndHandler(event)">
</esriraw>
<esri:GeometryService id="myGeometryService"
url="http://sampleserver2.arcgisonline.com/ArcGIS/rest/services/Geometr
y/GeometryServer"/> <mxanel width="100%" height="100%">
<mx:Button label="buffer" click="drawToolbar.activate
(Draw.MAPPOINT)"/>
<esri:Map id="map" width="100%" height="100%">
<esri:ArcGISDynamicMapServiceLayer
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Special
ty/ESRI_StatesCitiesRivers_USA/MapServer" />
<esri:GraphicsLayer id="myGraphicsLayer"/>
</esri:Map>
</mxanel>
</mx:Application>
原文地址:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=35667&extra=page%3D4%26amp%3Borderby%3Ddateline