首先来看一下实现点缓冲区分析的效果图吧!
实现步骤如下:
1、 分别添加html控件:Select、Text,两个 Button,;ID分别是selLayer,txtDistance,btnDistance,ClearBuffer。例外还添加一个DIV,并且在DIV中加入一个GridVview控件,ID分别是griddiv,GridView1,其中GridView1用来显示与缓冲区相交的图层要素的属性。如上图所示。
2、 在工具栏上添加一个tool工具,其javascript代码如下:<esri:ToolDefaultImage="~/images/Buffer.jpg"JavaScriptFile=""DisabledImage="~/images/Buffer.gif"ServerActionAssembly="App_Code" HoverImage="~/images/Buffer.gif"ServerActionClass="BufferTool" ClientAction="Point" ToolTip="缓冲区"SelectedImage="~/images/Buffer.png" Name="Buffer" Text="Buffer"></esri:Tool>
3、其他的一些地图控件像Map,MapResourceManager,ToolBar,Toc,ScaleBar等,自己添加并设置关联即可。
4、分别单击连个button,其click事件的代码如下:<scriptlanguage="javascript" type="text/javascript">
function btnDistance_onclick()
{
Var v=document.getElementById("txtDistance").value+"," +document.getElementById("selLayer").value;
//生成请求字符串
var argument = "ControlID=Map1&ControlType=Map&Type=Distance&EventArg="
var context = "Map";
//m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码
var
//用eval执行字符串
eval(script);
}
function ClearBuffer_onclick()
{
//生成请求字符串
var argument = "ControlID=Map1&ControlType=Map&Type=BufferClear&EventArg=" + "";
var context = "Map";
//m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码
var
//用eval执行字符串
eval(script);
}
</script>
5、 后台的.cs文件,代码如下: publicpartial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
public string
private string returnstring = "";
publicstring m_Callback = "";
protectedvoid Page_Load(object sender, EventArgs
{
//生成客户端脚本段
"argument", "processCallbackResult", "context", true);
}
#region成员
private string
//负责把结果回传给客户端
string ICallbackEventHandler.GetCallbackResult()
{
return
return
}
//负责从客户端javascript传来的参数
void ICallbackEventHandler.RaiseCallbackEvent(string
{
_callbackArg = eventArgument;
}
public void RaiseCallbackEvent( string
{
string v = "";
NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
if (keyValColl["Type"].ToString() == "Distance")
{
string str = keyValColl["EventArg"];
string []str1=str.Split(',');
"BufferDistance"]=str1[0];
"Layer"] = str1[1];
}
else if (keyValColl["Type"].ToString() == "BufferClear")
{
IEnumerable
MapResource gResource = null;
foreach (IGISFunctionality gfunc in
{
if (gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
{
MapResource)gfunc.Resource;
gResource.Graphics.Clear();
}
}
Map1.Refresh();
object[] oa = new object[1];
string showtable = "'hidden'";
string sa = "var griddiv = document.getElementById('griddiv');";
"griddiv.style.visibility = " + showtable + ";";
oa[0] = sa;
oa[0] = sa;
CallbackResult cr1 = new CallbackResult(null, null, "javascript", oa);
Map1.CallbackResults.Add(cr1);
v = Map1.CallbackResults.ToString();
return
}
}
6、接下来,添加一个类文件命名为:BufferTool.cs,其中的代码如下:
publicclass
{
#region IMapServerToolAction Members
void
{
ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;
try
{
PointEventArgs pointargs = (PointEventArgs)args;
System.Drawing.Point screenpoint = pointargs.ScreenPoint;
int)mapctrl.Width.Value, (int)mapctrl.Height.Value);
string strbd = mapctrl.Page.Session["BufferDistance"].ToString();
float
if (!Single.TryParse(strbd, out
{
bufferdistance = 0.0F;
}
new
float)mappoint.X - (bufferdistance / 2), (float)mappoint.Y - (bufferdistance / 2), bufferdistance, bufferdistance);
new
translateMatrix.Translate(0, 0);
float flattening = bufferdistance / 1000; //0.005F; //decimal degrees --- 100F; //meters,feet
gpath.Flatten(translateMatrix, flattening);
new
foreach (System.Drawing.PointF dpnt in
{
new
}
new
ring.Points = pc;
new
rings.Add(ring);
new
mappoly.Rings = rings;
IEnumerable
null;
foreach (IGISFunctionality gfunc in
{
if (gfunc.Resource.Name == "Buffer")
{
gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
}
}
if (gResource == null)
throw new Exception("Buffer Graphics layer not in MapResourceManager");
null;
foreach (System.Data.DataTable dt in
{
if (dt is
{
glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
break;
}
}
if (glayer == null)
{
new
gResource.Graphics.Tables.Add(glayer);
}
glayer.Clear();
ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)mappoly;
new
ge.Symbol.Transparency = 70.0;
glayer.Add(ge);
// Construct selection layer
string targetlayername = (string)mapctrl.Page.Session["Layer"];
int
int
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)mapctrl.GetFunctionality(resource_index);
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
if
{
typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
string[] lids;
string[] lnames;
null, out lids, out
for (int
{
if
{
layer_index = i;
break;
}
}
new
true;
spatialfilter.MaxRecords = 1000;
spatialfilter.Geometry = mappoly;
null, lids[layer_index], spatialfilter);
#region Graphics Converter Usage
/*** To use the ADF Converter to create a Graphics layer from DataTable, use the following code
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer graphicslayer = Converter.ToGraphicsLayer(datatable, Color.Yellow, Color.Yellow);
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource selresource = null;
foreach (IGISFunctionality gfunc in gfc)
{
if (gfunc.Resource.Name == "Selection")
{
selresource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
selresource.Graphics.Tables.Clear();
}
}
if (selresource == null)
return;
selresource.Graphics.Tables.Add(graphicslayer);
//*/
#endregion
#region Explicit Graphics Layer Usage
//*** To work with a Graphics Layer explicitly, and manage it within a Graphics dataset, use the following code
null;
foreach (IGISFunctionality gfunc in
{
if (gfunc.Resource.Name == "Selection")
{
selresource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
}
}
if (selresource == null)
return;
null;
foreach (System.Data.DataTable dt in
{
if (dt is
{
slayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
break;
}
}
if (slayer == null)
{
new
selresource.Graphics.Tables.Add(slayer);
}
slayer.Clear();
DataRowCollection drs = datatable.Rows;
int
for (int
{
if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
{
shpind = i;
break;
}
}
try
{
foreach (DataRow dr in
{
ESRI.ArcGIS.ADF.Web.Geometry.Geometry sgeom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)dr[shpind];
new
sge.Symbol.Transparency = 50.0;
slayer.Add(sge);
}
}
catch
{
throw new Exception("No geometry available in datatable");
}
//*/
#endregion
//string cbxvalue = (string)mapctrl.Page.Session["CheckBox1Value"];
"GridView1");
// Is gridview div visible or hidden
object[] oa = new object[1];
string showtable = "'hidden'";
//if (bool.Parse(cbxvalue))
//{
// display table of selected attributes
gdview.DataSource = datatable;
gdview.DataBind();
string returnstring = null;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
new
gdview.RenderControl(htw);
htw.Flush();
returnstring = sw.ToString();
}
new CallbackResult("div", "griddiv", "innercontent", returnstring);
mapctrl.CallbackResults.Add(cr);
"'visible'";
// set visibility of griddiv
string sa = "var griddiv = document.getElementById('griddiv');";
"griddiv.style.visibility = " + showtable + ";";
oa[0] = sa;
new CallbackResult(null, null, "javascript", oa);
mapctrl.CallbackResults.Add(cr1);
if
{ mapctrl.Refresh(); }
else if
{
mapctrl.RefreshResource(gResource.Name);
mapctrl.RefreshResource(selresource.Name);
}
}
}
catch (Exception
{
mapctrl.Refresh();
"Exception: "
return;
}
}
#endregion
}
7、差不多就是这些了,一些命名空间自己添加吧。运行下,看效果吧。
一起学习GIS及其二次开发,一起进步!