缓冲区空间查询
下面这个例子是在地图单击,然后做缓冲区,并找到跟这个缓冲区相交的面
LocalGeometryService pLocalGeometryService;
GeometryService pGeometryService;
QueryTask pQueryTask;
GraphicsLayer pBuffResultGraphicsLayer;
GraphicsLayer pBufferGraphicsLayer;
//该代码是用来创建几何服务对象,查询对象和创建本地动态地图服务和为地图加载图层
private void btnGeo_Click(objectsender,RoutedEventArgse)
{
//获取要查询的URL,构造QueryTask对象
pQueryTask = new QueryTask();
//使用QueryTaskLocalExtentions及创建了本地地图服务,又构造了QueryTask
LocalMapService pLocalMap = QueryTaskLocalExtensions.InitializeWithLocalService(pQueryTask,"radialflow.mpk", 2);
ArcGISLocalDynamicMapServiceLayerpLocalDymLayer =newArcGISLocalDynamicMapServiceLayer();
pLocalDymLayer.Service =pLocalMap;
_mapControl.Layers.Add(pLocalDymLayer);
pBufferGraphicsLayer =newGraphicsLayer();
//缓冲区查询
pBuffResultGraphicsLayer =newGraphicsLayer();
_mapControl.Layers.Add(pBuffResultGraphicsLayer);
_mapControl.Layers.Add(pBufferGraphicsLayer);
pLocalGeometryService =LocalGeometryService.GetService();
//创建GoemtryService,用来设置Buff
pGeometryService = new GeometryService(pLocalGeometryService.UrlGeometryService);
pGeometryService.BufferCompleted +=newEventHandler<GraphicsEventArgs>(GeometryService_BufferCompleted);
pGeometryService.Failed +=newEventHandler<TaskFailedEventArgs>(GeometryService_Failed);
pQueryTask.ExecuteCompleted +=newEventHandler<QueryEventArgs>(QueryTask_ExecuteCompleted);
pQueryTask.Failed +=newEventHandler<TaskFailedEventArgs>(GeometryService_Failed);
}
void GeometryService_BufferCompleted(object sender, GraphicsEventArgs args)
{
Graphic bufferGraphic = new Graphic();
bufferGraphic.Geometry =args.Results[0].Geometry;
bufferGraphic.Symbol =Layout.Resources["BufferSymbol"]asESRI.ArcGIS.Client.Symbols.Symbol;
pBufferGraphicsLayer.Graphics.Add(bufferGraphic);
//查找
ESRI.ArcGIS.Client.Tasks.Queryquery = new ESRI.ArcGIS.Client.Tasks.Query();
query.ReturnGeometry =true;
query.OutSpatialReference =newSpatialReference(4326);
query.Geometry =bufferGraphic.Geometry;
query.OutFields.Add("OBJECTID");
query.SpatialRelationship =SpatialRelationship.esriSpatialRelIntersects;
pQueryTask.ExecuteAsync(query);
}
private void QueryTask_ExecuteCompleted(objectsender,QueryEventArgsargs)
{
pBuffResultGraphicsLayer.ClearGraphics();
if (args.FeatureSet.Features.Count < 1)
{
MessageBox.Show("没有找到要素");
return;
}
foreach (GraphicselectedGraphicinargs.FeatureSet.Features)
{
selectedGraphic.Symbol =Layout.Resources["ParcelSymbol"]asESRI.ArcGIS.Client.Symbols.Symbol;
pBuffResultGraphicsLayer.Graphics.Add(selectedGraphic);
}
_mapControl.Extent =pBuffResultGraphicsLayer.FullExtent;
}
private void GeometryService_Failed(objectsender,TaskFailedEventArgsargs)
{
MessageBox.Show("Geometry service failed: " +args.Error);
}
void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Query failed: " +e.Error);
}
//在地图上点击,然后创建缓冲区做空间查询
private void _mapControl_MouseClick(objectsender,Map.MouseEventArgse)
{
pGeometryService.CancelAsync();
//获取鼠标位置
Graphic clickGraphic = new Graphic();
clickGraphic.Symbol =Layout.Resources["DefaultMarkerSymbol"]asESRI.ArcGIS.Client.Symbols.Symbol;
clickGraphic.Geometry =e.MapPoint;
// 空间参考
clickGraphic.Geometry.SpatialReference =newSpatialReference(4326);
pBufferGraphicsLayer.ClearGraphics();
clickGraphic.SetZIndex(2);
pBufferGraphicsLayer.Graphics.Add(clickGraphic);
// 设置缓冲区参数
ESRI.ArcGIS.Client.Tasks.BufferParametersbufferParams = newESRI.ArcGIS.Client.Tasks.BufferParameters()
{
BufferSpatialReference =newSpatialReference(4326),
OutSpatialReference =newSpatialReference(4326),
Unit = LinearUnit.Meter,
};
bufferParams.Distances.Add(0.1);
bufferParams.Features.Add(clickGraphic);
pGeometryService.BufferAsync(bufferParams);
}
![](https://img-my.csdn.net/uploads/201205/02/1335922937_2133.jpg)