ArcgisServer9.3 中矩形选择查询的实现(利用callback机制)
效果如下
1、首先加入一个html的Button控件, 同时实现本博客中的一个下拉列表控件无刷新的控制另外一个下拉列表控件(利用Coolite.Ext控件);例外添加一个Gridview控件并将它放在一个div中,同时将它们放在一个updatepane中;如下所示:
<input id="Button3"type="button"
value="矩形框选" onclick="selectRectangle()" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div id="griddiv" style=" overflow:auto; width:240px; height:300px;" >
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AutoGenerateColumns="true" OnPageIndexChanging="GridView1_PageIndexChanging" >
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
2、在前台用javascript实现拉一个矩形框的操作,如下:
<script language="javascript" type="text/javascript">
//在地图上拉一个矩形框
function selectRectangle() {
var map = $find('Map1');
map.set_mouseMode(ESRI.ADF.UI.MouseMode.Custom);
map.getGeometry(ESRI.ADF.Graphics.ShapeType.Envelope,useRectangle, null, 'red', '#0000FF', 'crosshair', true);
}
//拉框完成后实现的操作
function useRectangle(inputGeometry) {
var map = $find('Map1');
var env = inputGeometry.getEnvelope();
var griddiv = document.getElementById("griddiv");
var Xmin = env.get_xmin();
var Ymin = env.get_ymin();
var Xmax = env.get_xmax();
var Ymax = env.get_ymax();
//调用触发callball的事件
SetCustomOperation("QueryRectangle"+","+Xmin+","+Ymin+","+Xmax+","+Ymax);
}
function SetCustomOperation(sVal)
{
var message ='';
message +=',' + sVal;
var context ='Map1';
<%=sCallBack%>
}
</script>
3、在后台的Default.aspx.cs 下的代码如下:
public partial class _Default :System.Web.UI.Page,ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
public string sCallBack = string.Empty;
sCallBack = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);
}
}
4、实现矩形框选择的查询代码如下:
public void QueryRectangle(double Xmin,double Ymin,double Xmax,double Ymax)
{
try
{
ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(Xmin, Ymin, Xmax, Ymax);
//获取所选地图资源与图层
string strRes = Session["ResName"].ToString();
string strLayer = Session["LayerName"].ToString();
ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisfunctionality = Map1.GetFunctionality(strRes);
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = gisfunctionality.Resource;
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
if (supported)
{
ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc;
qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
gisresource.CreateFunctionality( typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
string[] lids;
string[] lnames;
qfunc.GetQueryableLayers(null, out lids, out lnames);
ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
spatialfilter.ReturnADFGeometries = true;
spatialfilter.MaxRecords = 10000;
spatialfilter.Geometry = env;
for (int i = 0; i < lids.Length; i++)
{
if (lnames[i] == strLayer)
{
DataTable datatable = qfunc.Query(gisfunctionality.Name, lids[i], spatialfilter);
datatable.TableName = gisresource.Name + "_" + lnames[i];
if (datatable.Rows.Count > 0)
{
string datasetName = string.Format("选择要素 - {0}", datatable.TableName);
System.Data.DataSet dataset = new System.Data.DataSet(datasetName);
dataset.Tables.Add(datatable);
this.GridView1.DataSource = dataset;
this.GridView1.DataBind();
Session["Gridview1"] = dataset;
string returnstring = null;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
htw.Flush();
returnstring = sw.ToString();
}
//innercontent相当于innerhtml
CallbackResult cr = new CallbackResult("div", "griddiv", "innercontent", returnstring);
//通过回调将信息从服务器端传输到客户端
Map1.CallbackResults.Add(cr);
smapstring = Map1.CallbackResults.ToString();
break;
}
else
{
object[] oa = new object[1];
oa[0] = "Ext.MessageBox.alert('提示','未查询到任何信息');";
CallbackResult rst = new CallbackResult(Page, "javascript", oa);
Map1.CallbackResults.Add(rst);
smapstring = Map1.CallbackResults.ToString();
}
}
}
}
}
catch (Exception ex)
{
SystemLog.WriteLog("矩形选择要素出错!", ex);
}
}
5、然后就是ICallbackEventHandler接口的实现。
public string GetCallbackResult()
{
return smapstring;
}
public void RaiseCallbackEvent(string eventArgument)
{
try
{
if (eventArgument.Contains("QueryRectangle"))
{
string a = eventArgument;
string[] str = a.Split(',');
double Xmin=Convert.ToDouble(str[2]);
double Ymin=Convert.ToDouble(str[3]);
double Xmax=Convert.ToDouble(str[4]);
double Ymax=Convert.ToDouble(str[5]);
QueryRectangle(Xmin, Ymin, Xmax, Ymax);
}
}
catch (Exception ex)
{
systemLog.WriteLog("矩形选择查询出现错误", ex);
}
}
6、例外还有GridView分页的实现。
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
DataSet DS = Session["GridView1"] as DataSet;
this.GridView1.DataSource = DS;
this.GridView1.DataBind();
}