效果图如下所示:
1、在工具栏上添加idenfify工具,其代码如下:
<esri:Tool ClientAction="Point" DefaultImage="~/image/identify.png"
DisabledImage="~/image/identify_OFF.gif"HoverImage="~/image/identify_HOVER.gif"JavaScriptFile=""Name="MapIdentify"SelectedImage="~/image/identify_ON.gif" ServerActionAssembly="App_code" ServerActionClass="MapIdentify" Text="MapIdentify" ToolTip="MapIdentify" />
2、添加TaskResults控件用来存放结果并将它放在一个div中。代码如下:
<div id="tskdiv" runat="server" >
<esri:TaskResults ID="TaskResults1" runat="server" BackColor="#ffffff"
Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000" Map="Map1" style="top: 253px; left: 517px; overflow:auto; width: 298px; height: 209px; "
/>
</div>
3.在Default 页面的Page_Load中加入
Session["TaskResultsControl"] = TaskResults1;
用来传递TaskResults1对象。
4、在App_code 中加入MapIdentify.cs,其代码如下:
public class MapIdentify:IMapServerToolAction
{
#region IMapServerToolAction Members
void IMapServerToolAction.ServerAction(ToolEventArgs args)
{
int m_numberDecimals = 3;
int m_IdentifyTolerance = 5;
Map map = (Map)args.Control;
IGISResource resource;
IQueryFunctionality query;
string locXString = "";
string locYString = "";
IdentifyOption m_idOption = IdentifyOption.VisibleLayers;
PointEventArgs pea = (PointEventArgs)args;
System.Drawing.Point screen_point = pea.ScreenPoint;
//像素坐标转换成地理坐标
ESRI.ArcGIS.ADF.Web.Geometry.Point mapPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screen_point.X, screen_point.Y, map.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
List<DataSet> gdsList = new List<DataSet>();
DataTable[] ds = null;
foreach (IMapFunctionality mapFunc in map.GetFunctionalities())
{
if (mapFunc is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)
{
continue;
}
resource = mapFunc.Resource;
//建立查询方法
query = resource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), "identify_") as IQueryFunctionality;
string[] layerIds;
string[] layerNames;
//查询地图图层id和名称
query.GetQueryableLayers(null, out layerIds, out layerNames);
//resource类型
string resourceType = resource.DataSource.GetType().ToString();
//只显示坐标的3个小数
double roundFactor = Math.Pow(10, m_numberDecimals);
string pointXString = Convert.ToString(Math.Round(mapPoint.X * roundFactor) / roundFactor);
string pointYString = Convert.ToString(Math.Round(mapPoint.Y * roundFactor) / roundFactor);
locXString = pointXString;
locYString = pointYString;
try
{
//进行查询并且把查询结果放入DataTable数组
// 查询坐标 冗余半径 只查可见图层
ds = query.Identify(mapFunc.Name, mapPoint, m_IdentifyTolerance, m_idOption, null);
}
catch (Exception e)
{
//出错处理
DataTable table = new DataTable();
table.TableName = "Identify Error: " + e.Message;
ds = new DataTable[] { table };
}
//判断查询结果
if (ds != null && ds.Length > 0)
{
DataSet gds = new DataSet();
DataTable table;
//对查询结果DataTable[]进行遍历添加到DataSet
for (int j = ds.Length - 1; j >= 0; j--)
{
table = ds[j];
if (table.Rows.Count == 0 && table.TableName.IndexOf("Error") < 0)
{
//跳过本次进入下一次循环
continue;
}
//把DataTable转换成GraphicsLayer
GraphicsLayer layer = ESRI.ArcGIS.ADF.Web.Converter.ToGraphicsLayer(table, System.Drawing.Color.Empty, System.Drawing.Color.Aqua);
layer.RenderOnClient = true;
if (layer != null)
{
gds.Tables.Add(layer);
}
else
{
gds.Tables.Add(table);
}
}
if (gds.Tables.Count == 0)
{
//跳过本次循环
continue;
}
gds.DataSetName = resource.Name + " (" + pointXString + ", " + pointYString + ")";
gdsList.Add(gds);
}
}
ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults taskResults =
(ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults)map.Page.Session["TaskResultsControl"];
taskResults.Visible = true;
for (int i = gdsList.Count - 1; i >= 0; i--)
{
//用TaskResults进行查询内容显示
// m_resultsDisplay.DisplayResults(null, null, null, gdsList[i]);
taskResults.DisplayResults(null, null, null, gdsList[i]);
}
//没有查询到结果
if (gdsList.Count == 0)
{
string heading = "Location (" + locXString + ", " + locYString + ") No results found";
string detail = "No results found";
SimpleTaskResult str = new SimpleTaskResult(heading, detail);
taskResults.DisplayResults(null, null, null, str);
}
// Copy the TaskResults' callback results to the Map so the results show up
map.CallbackResults.CopyFrom(taskResults.CallbackResults);
}
#endregion
}