using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Data;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ADF.Web.DataSources.IMS;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
public string m_Callback = "";
private string _callbackArg;
protected void Page_Load(object sender, EventArgs e)
{
//生成客户端脚本段
m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);
}
string ICallbackEventHandler.GetCallbackResult()
{
return RaiseCallbackEvent(_callbackArg);
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
_callbackArg = eventArgument;
}
private string RaiseCallbackEvent(string _callbackArg)
{
string v = "";
NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
if (keyValColl["Type"].ToString() == "query")
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string Input = keyValColl["EventArg"];
string str = "";
string targetlayername = "States";
//获取Data Layers的MapFunctionality
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(0);
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
//是否支持QueryFunctionality
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
if (supported)
{
//创建QueryFunctionality
ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
string[] lids;
string[] lnames;
//查询图层的id和名称
qfunc.GetQueryableLayers(null, out lids, out lnames);
int layer_index = 0;
//获取目标图层的index
for (int i = 0; i < lnames.Length; i++)
{
if (lnames[i] == targetlayername)
{
layer_index = i;
break;
}
}
//空间过滤
ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
//是否返回地理元素
spatialfilter.ReturnADFGeometries = false;
//返回最大记录数
spatialfilter.MaxRecords = 1000;
spatialfilter.Geometry = Map1.GetFullExtent();
//查询目标图层并且把结果存入DataTable
//查询语句
spatialfilter.WhereClause = "STATE_NAME LIKE '" + Input + "%'";
System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);
//组成显示内容
for (int i = 0; i < datatable.Rows.Count; i++)
{
string idv = datatable.Rows[i]["OBJECTID"].ToString();
string namev = datatable.Rows[i]["STATE_NAME"].ToString();
if (i == 0)
{
str = "<a href='#' οnclick=gocenger('" + idv + "')>" + namev + "</a>";
}
else
{
str = str + "<br/>" + "<a href='#' οnclick=gocenger('" + idv + "')>" + namev + "</a>";
}
}
}
sb.AppendFormat("div:::{0}:::innercontent :::{0}", "list1", str);
v = sb.ToString();
else if (keyValColl["Type"].ToString() == "center")
{
string Input = keyValColl["EventArg"];
string str = "";
string targetlayername = "States";
//获取Data Layers的MapFunctionality
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(0);
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
//是否支持QueryFunctionality
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
if (supported)
{
//创建QueryFunctionality
ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
string[] lids;
string[] lnames;
//查询图层的id和名称
qfunc.GetQueryableLayers(null, out lids, out lnames);
int layer_index = 0;
//获取目标图层的index
for (int i = 0; i < lnames.Length; i++)
{
if (lnames[i] == targetlayername)
{
layer_index = i;
break;
}
}
//空间过滤S
ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
//是否返回地理元素
spatialfilter.ReturnADFGeometries = true;
//返回最大记录数
spatialfilter.MaxRecords = 1000;
spatialfilter.Geometry = Map1.GetFullExtent();
//查询目标图层并且把结果存入DataTable
//查询语句
spatialfilter.WhereClause = "OBJECTID = " + Input;
System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);
DataRowCollection drs = datatable.Rows;
int shpind = -1;
for (int i = 0; i < datatable.Columns.Count; i++)
{
if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
{
shpind = i;
break;
}
}
ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer glayer = null;
glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)datatable;
foreach (DataRow dr in drs)
{
//获取Geometry
ESRI.ArcGIS.ADF.Web.Geometry.Geometry geo = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)dr[shpind];
//获取查询到的这个Geometry的中心点
ESRI.ArcGIS.ADF.Web.Geometry.Point centerpoint = ESRI.ArcGIS.ADF.Web.Geometry.Geometry.GetCenterPoint(geo);
//以这个中心点重新设置地图的中心
Map1.CenterAt(centerpoint);
ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge2 = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geo, System.Drawing.Color.Red);
ge2.Symbol.Transparency = 50;
//添加到Buffer中进行显示
//glayer.Add(ge2);
//把CallbackResults字符串返回客户端用来更新地图显示
v = Map1.CallbackResults.ToString();
}
}
}
return v;
}
}
.........................................................................................................................................................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="ESRI.ArcGIS.ADF.Web.UI.WebControls, Version=10.0.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86"
Namespace="ESRI.ArcGIS.ADF.Web.UI.WebControls" TagPrefix="esri" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr>
<td colspan="2">
输入关键字
<input id="Text1" type="text" value="A" />
<input id="Button1" style="width: 61px" type="button" value="查询" οnclick="search()" runat="server" />
</td>
</tr>
<tr>
<td style="width: 483px">
<esri:Map ID="Map1" runat="server" Height="393px" MapResourceManager="MapResourceManager1"
Width="478px">
</esri:Map>
</td>
<td valign="top" style="width: 150px">
查询结果:<br />
<div id="list1" >
</div>
</td>
</tr>
</table>
<esri:MapResourceManager ID="MapResourceManager1" runat="server">
<ResourceItems>
<esri:MapResourceItem Definition="<Definition DataSourceDefinition="wxw" DataSourceType="ArcGIS Server Local" Identity="To set, right-click project and 'Add ArcGIS Identity'" ResourceDefinition="Layers@usa" />"
DisplaySettings="visible=True:transparency=0:mime=True:imgFormat=PNG8:height=100:width=100:dpi=96:color=:transbg=False:displayInToc=True"
Name="MapResourceItem0" />
</ResourceItems>
</esri:MapResourceManager>
</div>
<script type="text/javascript" language="javascript">
function search() {
//获取输入的查询关键字
var v=document.getElementById("Text1").value;
//生成请求字符串
var argument = "ControlID=Map1&ControlType=Map&Type=query&EventArg=" + v;
var context = "Map1";
//m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码
var script=<%= m_Callback %>
//用eval执行字符串
eval(script);
}
function gocenger(id) {
//请求字符串
var argument = "ControlID=Map1&ControlType=Map&Type=center&EventArg=" + id;
var context = "Map1";
var script=<%= m_Callback %>
eval(script);
}
// Function called when the callback processing is completed on the server.
// This is where the server-side results are processed on the client.
// function processCallbackResult(callbackResult, context) {
// // Get the HTML div element that we wish to update
// var timeDiv = document.getElementById('list1');
// // Update the div to display the result of the callback
// timeDiv.innerHTML = callbackResult;
// }
// // Function called when there is an error in callback processing on the server
// function postMyError(callbackResult, context) {
// // Display the information returned from the server to the user
// alert("Callback Error: " + callbackResult + ", " + context);
// }
</script>
</form>
</body>
</html>