目的:
在地图上点击一个点生成一个缓冲区。
1、制作GP工具:
GP工具制作按照http://help.arcgis.com/zh-cn/arcgisdesktop/10.0/help/index.html#//002v00000014000000来做。
2、发布GP工具:
3、分析GP服务:
将发布的GP服务地址在浏览器中输入:http://wade-pc/arcgis/rest/services/BufferService/GPServer/Buffer%20Points
4、在Silverlight中使用GP服务:
前台代码:
<UserControl xmlns:esri="http://schemas.esri.com/arcgis/client/2009" x:Class="GPServiceTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<!--输入点样式-->
<esri:SimpleMarkerSymbol x:Key="DefaultClickSymbol" Size="5" Color="Red"/>
<!--缓冲区样式-->
<esri:SimpleFillSymbol x:Key="DefaultBufferSymbol" Fill="#660000FF" BorderBrush="Blue" BorderThickness="2" />
</Grid.Resources>
<esri:Map x:Name="MyMap" Background="White"
MouseClick="MyMap_MouseClick" >
<!--底图-->
<esri:ArcGISDynamicMapServiceLayer ID="main"
Url="http://www.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetWarm/MapServer"/>
<!--存放输入点-->
<esri:GraphicsLayer ID="MyGraphicsLayer">
</esri:GraphicsLayer>
</esri:Map>
<Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,15,0" >
<Rectangle Fill="#77919191" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="0,0,0,5" >
<Rectangle.Effect>
<DropShadowEffect/>
</Rectangle.Effect>
</Rectangle>
<Rectangle Fill="#FFFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15" />
<TextBlock x:Name="InformationText" Text="Click on map to set a location. A buffer of 1000 meters will be displayed."
Width="200" Margin="30,20,30,25" HorizontalAlignment="Left" TextWrapping="Wrap" />
</Grid>
</Grid>
</UserControl>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
namespace GPServiceTest
{
public partial class MainPage : UserControl
{
Geoprocessor _geoprocessorTask;
public MainPage()
{
InitializeComponent();
_geoprocessorTask = new Geoprocessor("http://wade-pc/arcgis/rest/services/BufferService/GPServer/Buffer%20Points");
_geoprocessorTask.JobCompleted += new EventHandler<JobInfoEventArgs>(_geoprocessorTask_JobCompleted);
_geoprocessorTask.GetResultDataCompleted += new EventHandler<GPParameterEventArgs>(_geoprocessorTask_GetResultDataCompleted);
_geoprocessorTask.Failed += new EventHandler<TaskFailedEventArgs>(_geoprocessorTask_Failed);
}
void _geoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
{//分析失败
MessageBox.Show("失败");
}
void _geoprocessorTask_GetResultDataCompleted(object sender, GPParameterEventArgs e)
{
//将分析结果显示在地图上
GraphicsLayer graphicsLayer = new GraphicsLayer();
GPFeatureRecordSetLayer featureSetLayer = e.Parameter as GPFeatureRecordSetLayer;
foreach (Graphic graphic in featureSetLayer.FeatureSet.Features)
{
graphic.Symbol = LayoutRoot.Resources["DefaultBufferSymbol"] as Symbol;
graphicsLayer.Graphics.Add(graphic);
}
MyMap.Layers.Add(graphicsLayer);
}
void _geoprocessorTask_JobCompleted(object sender, JobInfoEventArgs e)
{
if (e.JobInfo.JobStatus == esriJobStatus.esriJobFailed)
{
MessageBox.Show("服务请求失败:" + e.JobInfo.Messages.ToString());
}
else
{
_geoprocessorTask.GetResultDataAsync(e.JobInfo.JobId, "OutputPolygons");//异步获取分析结果
}
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{//鼠标点击
//清除存放点击点的图层
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
//获取点击的点 并添加到地图中
e.MapPoint.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new Graphic()
{
Geometry = e.MapPoint,
Symbol = LayoutRoot.Resources["DefaultClickSymbol"] as Symbol
};
graphic.SetZIndex(1);
graphicsLayer.Graphics.Add(graphic);
//分析
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input_Features", e.MapPoint));//设置输入参数
parameters.Add(new GPLinearUnit("Distance", esriUnits.esriMeters, 10000));//设置缓冲半径
_geoprocessorTask.SubmitJobAsync(parameters);//提交异步分析
}
}
}
5、结果展示: