default.aspx页面添加点查询控件:
<esri:Tool BuddyItem="Tool" ClientAction="Point" JavaScriptFile="" Name="MapIdentify"
ServerActionAssembly="App_Code" ServerActionClass="IdentifyPoint" Text="Identify"
ToolTip="点查询" DefaultImage="Images/MapTool/Info1.gif" HoverImage="Images/MapTool/Info2.gif"
SelectedImage="Images/MapTool/Info2.gif" />
然后在App_Code里面添加IdentifyPoint类,实现代码如下:
//添加的引用
using ESRI.ArcGIS.ADF.Web;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.Geometry;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
/// <summary>
///IdentifyPoint 实现点查询
/// </summary>
public class IdentifyPoint : IMapServerToolAction
{
public IdentifyPoint()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public void ServerAction(ToolEventArgs args)
{
//从方法的参数中得到地图控件的引用
Map map = args.Control as Map;
//从方法的参数中得到用户单击位置的点
PointEventArgs pea = (PointEventArgs)args;
System.Drawing.Point screen_point = pea.ScreenPoint;
//将屏幕像素坐标转换到地图坐标
Point point = Point.ToMapPoint(screen_point.X, screen_point.Y, map.Extent, (int)map.Width.Value, (int)map.Height.Value);
//利用该点查询要素,要实现查询,则需要判断资源是否具有查询功能
//调用GetFunctionality方法,得到改资源能提供的功能,gisfunc不为空表示能提供查询,地图显示等功能,则接着执行,否则直接返回
IGISFunctionality gisfunc = map.GetFunctionality("Portland");
if (gisfunc == null)
return;
IGISResource gisresource = gisfunc.Resource;
//判断是否具有查询功能,如果没有查询,则直接返回,如果有,则调用资源对象的CreateFunctionality方法得到查询功能对象
bool supportquery = gisresource.SupportsFunctionality(typeof(IQueryFunctionality));
if (!supportquery)
return;
IQueryFunctionality qfunc;
qfunc = gisresource.CreateFunctionality(typeof(IQueryFunctionality), null) as IQueryFunctionality;
//得到查询功能对象后,调用Identify方法执行查询(第一个参数是地图功能名称,第二个参数是查询要素的图形对象,
//第三个参数是误差范围,第四个参数是查询方式,第五个参数是可查询图层的ID,如果是AllLayers,则查询图层的ID为null)
System.Data.DataTable[] qdatatable = qfunc.Identify(null, point, 1, IdentifyOption.AllLayers, null);
if (qdatatable == null)
return;
System.Data.DataSet dataset = new System.Data.DataSet();
for (int i = 0; i < qdatatable.Length; i++)
{
dataset.Tables.Add(qdatatable[i]);
}
DataTableCollection dtc = dataset.Tables;
//调用IdentifyHelper类中的ShowIdentifyResult方法
IdentifyHelper.ShowIdentifyResult(map, dtc);
}
然后在App_Code里面添加IdentifyHelper类,实现代码如下:
/// <summary>
/// 得到查询的结果
/// </summary>
/// <param name="map"></param>
/// <param name="dtc"></param>
public static void ShowIdentifyResult(Map map, DataTableCollection dtc)
{
string returnstring = string.Empty;
foreach (DataTable dt in dtc)
{
if (dt.Rows.Count == 0)
continue;
returnstring += GetHtmlFromDataTable(dt);
}
HighLighShow(map, dtc);
returnstring = returnstring.Replace("\r\n", "");
returnstring = returnstring.Replace("\n", "");
string functionValue = "var theForm=document.forms[0];";
functionValue += "theForm.FunctionValue.Value='" + returnstring + "';";
functionValue += "open('IdentifyResult.htm','IdentifyResult');";//需要在项目中添加IdentifyResult.htm
AddJavaScriptCallback(map, functionValue);
}
/// <summary>
/// 根据数据源生成页面HTML代码
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetHtmlFromDataTable(DataTable dt)
{
GridView gd = new GridView();
gd.ToolTip = dt.TableName;
gd.Caption = dt.TableName;
gd.DataSource = dt;
gd.DataBind();
gd.Visible = true;
gd.BorderWidth = 0;
gd.CssClass = "list-line";
gd.CellPadding = 3;
gd.CellSpacing = 1;
gd.HeaderStyle.CssClass = "barbg";
gd.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gd.RowStyle.CssClass = "listbg";
string returnString = string.Empty;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
gd.RenderControl(htw);
htw.Flush();
string tempStr = sw.ToString();
returnString += tempStr;
}
return returnString;
}
/// <summary>
/// 高亮显示要素
/// </summary>
/// <param name="map"></param>
/// <param name="dtc"></param>
/// 在写此功能时需要添加using ESRI.ArcGIS.ADF.ArcGISServer;using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;这两个命名空间
/// 此时需要先右键项目,选择Add Arcgis Reference 命令,添加上面两个程序集
public static void HighLighShow(Map map, DataTableCollection dtc)
{
//得到Portland的绘图功能
MapFunctionality mf = (MapFunctionality)map.GetFunctionality("Portland");
MapDescription mapDescription = mf.MapDescription;
//将原来高亮显示的要素去掉高亮
mapDescription.CustomGraphics = null;
//创建面符号
SimpleFillSymbol sfs = CreateSimpleFillSymbol();
//绘制每个选中要素
foreach (DataTable dt in dtc)
{
if (dt.Rows.Count == 0)
continue;
HighLightPolygon(mapDescription, dt, sfs);
}
RefreshMap(map, "Portland");
}
/// <summary>
/// 刷新地图
/// </summary>
/// <param name="map"></param>
/// <param name="resourceName"></param>
/// ImageBlendingMode(地图图片融合方式属性),如果该值为WebTier刷新整个地图,否则只刷新指定的资源
public static void RefreshMap(Map map, string resourceName)
{
//web层融合
if (map.ImageBlendingMode == ImageBlendingMode.WebTier)
{
map.Refresh();
}
//浏览器端融合
else if (map.ImageBlendingMode == ImageBlendingMode.Browser)
{
map.RefreshResource(resourceName);
}
}
/// <summary>
/// 添加地图的回调方法
/// </summary>
/// <param name="map"></param>
/// <param name="executeStr"></param>
public static void AddJavaScriptCallback(Map map, string executeString)
{
object[] oa = new object[1];
oa[0] = executeString;
//CallbackResult可以将信息传回客户端,更新客户端页面的内容、图片或执行javascript脚本
CallbackResult cr = new CallbackResult(null, null, "javascript", oa);
map.CallbackResults.Add(cr);
}
在'IdentifyResult.htm'页面的代码如下:
<head>
<title>图形查询结果</title>
<script type="text/javascript">
function loadResult()
{
var identifyResult=opener.document.forms[0].FunctionValue.Value;
var o=document.getElementById("datadiv");
if(o!=null)
{
o.innerHTML=identifyResult;
}
}
</script>
</head>
<body>
<div runat="server" id="datadiv">
</div>
<script type="text/javascript">
function window.onload()
{
loadResult();
window.focus();
}
</script>
</body>