本例子使用GPK实现一个缓冲区分析,在地图上单击,以该点作为基点然后根据输入的缓冲距离做缓冲区分析,核心代码如下:
namespace ArcGISWpfLgp
{
public partial class MainWindow : Window
{
Geoprocessor pGPTask;
public MainWindow()
{
InitializeComponent();
//授权
ArcGISRuntime.SetLicense("许可");
//初始化服务器
LocalServer.Initialize();
//获取本地GP服务
LocalGeoprocessingService.GetServiceAsync(@"SimpleBuffer.gpk", GPServiceType.Execute, (gpService) =>
{
if (gpService.Error != null)
{
MessageBox.Show(gpService.Error.Message);
}
//创建Geoprocessor
pGPTask = new Geoprocessor(gpService.UrlGeoprocessingService + "/" +
"SimpleBuffer");
MyMap.MouseClick += MyMap_MouseClick;
});
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
pGPTask.CancelAsync();
GraphicsLayer pGraphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
pGraphicsLayer.ClearGraphics();
e.MapPoint.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = e.MapPoint,
Symbol = LayoutRoot.Resources["DefaultClickSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
};
graphic.SetZIndex(1);
pGraphicsLayer.Graphics.Add(graphic);
//构造GP的参数,2个,一个表示FeatureSet,一个表示缓冲距离
List<GPParameter> pGParams = new List<GPParameter>();
double pDist = System.Convert.ToDouble(txtBoxDist.Text);
Graphic pGraphic = new Graphic { Geometry = e.MapPoint };
FeatureSet pFeatureSet = new FeatureSet(new List<Graphic> { pGraphic });
pGParams.Add(new GPFeatureRecordSetLayer("InputFeatures", pFeatureSet));
pGParams.Add(new GPLinearUnit("Distance", esriUnits.esriKilometers, pDist));
pGPTask.ExecuteCompleted += (s, e1) =>
{
GPExecuteResults results = e1.Results;
GPFeatureRecordSetLayer rs = results.OutParameters[0] as GPFeatureRecordSetLayer;
Graphic graphicBuff = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = rs.FeatureSet.Features[0].Geometry,
Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
};
pGraphicsLayer.Graphics.Add(graphicBuff);
};
pGPTask.Failed += (s2, e2) =>
{
MessageBox.Show(e2.Error.Message);
};
pGPTask.ExecuteAsync(pGParams);
}
}
}
![](https://img-my.csdn.net/uploads/201205/02/1335923070_2488.jpg)