本文主要是介绍ArcGIS.Server.9.2.DotNet自带例子分析(一、三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作者:水的右边(GIS特邀嘉宾)
目的:
1.MapIdentify功能,自定义Tool以及TaskResults应用。
准备:
1.(一、二)的工程,具体见前篇。
开始:
1.编辑Toolbar1的ToolbarItems属性添加一个Tool,ClientAction属性为MapIdentify('Map1'); Name属性为MapIdentify Text属性为Identify ToolTip属性为Identify (Ctrl-MouseClick) 具体代码如下:
1<esri:Tool ClientAction="MapIdentify('Map1');" DefaultImage="~/Images/identify.png"
2HoverImage="~/Images/identify_HOVER.gif" JavaScriptFile="" Name="MapIdentify" SelectedImage="~/Images/identify_ON.gif" Text="Identify" ToolTip="Identify (Ctrl-MouseClick)"
/>
2.开始代码部分的编写,添加MapIdentify.cs,用来实现具体的功能。代码和说明如下:
代码:
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.Collections.Specialized;
using ESRI.ArcGIS.ADF.Web.DataSources;
using System.Collections.Generic;
using ESRI.ArcGIS.ADF.Web;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;
using System.Data;
namespace MappingApp
{
public class MapIdentify
{
private Page m_page;
private Map m_map;
//客户端脚本段
private string m_callbackInvocation ="";
//脚本路径
private string m_filePath ="";
//保留几位小数
private int m_numberDecimals =3;
//可见图层
private IdentifyOption m_idOption = IdentifyOption.VisibleLayers;
//查询的冗余范围半径
public int m_IdentifyTolerance =5;
//用来显示查询结果内容
private TaskResults m_resultsDisplay =null;private DataSet m_dataset;
public MapIdentify()
{
}
public MapIdentify(Map map)
{
if (map !=null)
{
m_map = map;
//生成客户端调用方法
SetupIdentify();
}
}
public MapIdentify(Map map, string filePath)
{
m_map = map;
m_filePath = filePath;
//生成客户端调用方法
SetupIdentify();
}
//生成客户端调用方法
public void SetupIdentify()
{
//获取Map控件所在的页面
m_page = m_map.Page;
System.Text.StringBuilder sb =new System.Text.StringBuilder();
//生产客户端的脚本段
m_callbackInvocation = m_page.ClientScript.GetCallbackEventReference(m_page, "message", "processCallbackResult", "context", true);
//引用display_mapidentify.js的文件
sb.Append("/n<script language=/"javascript/" type=/"text/javascript/" src=/"" + m_filePath + "JavaScript/display_mapidentify.js/" ></script>/n");
//
sb.Append("<script language=/"javascript/" type=/"text/javascript/">var identifyCallbackFunctionString = /"" + m_callbackInvocation + "/";</script>/n");
//把sb的脚本注册到页面中
if (!m_page.ClientScript.IsClientScriptBlockRegistered("IdentifyScript"))
{
m_page.ClientScript.RegisterClientScriptBlock(m_page.GetType(), "IdentifyScript", sb.ToString());
}
}
//具体的功能实现方法
public string Identify(NameValueCollection queryString)
{
//x,y的坐标
string xString = queryString["minx"];
string yString = queryString["miny"];
string locXString ="";
string locYString ="";
int x = Convert.ToInt32(xString);
int y = Convert.ToInt32(yString);
IGISResource resource;
IQueryFunctionality query;
//像素坐标转换成地理坐标
ESRI.ArcGIS.ADF.Web.Geometry.Point mapPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(x, y, m_map.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
List<DataSet> gdsList =new List<DataSet>();
foreach (IMapFunctionality mapFunc in m_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;
DataTable[] ds =null;
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);
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);
}
}
for (int i = gdsList.Count -1; i >=0; i--)
{
//用TaskResults进行查询内容显示
m_resultsDisplay.DisplayResults(null, null, null, gdsList);
}
//没有查询到结果
if (gdsList.Count ==0)
{
string heading ="Location ("+ locXString +", "+ locYString +") No results found";
string detail ="No results found";
SimpleTaskResult str =new SimpleTaskResult(heading, detail);
m_resultsDisplay.DisplayResults(null, null, null, str);
}
//返回结果
return m_resultsDisplay.CallbackResults.ToString();
}
public Map Map
{
get
{ return m_map; }
set
{ m_map = value; }
}
public Page Page
{
get
{ return m_page; }
set
{ m_page = value; }
}
public DataSet DataSet
{
get
{ return m_dataset; }
set
{ m_dataset = value; }
}
public IdentifyOption IdentifyOption
{
get
{ return m_idOption; }
set
{ m_idOption = value; }
}
public string ClientCallbackInvocation
{
get
{ return m_callbackInvocation; }
set
{ m_callbackInvocation = value; }
}
public string FilePath
{
get
{ return m_filePath; }
set
{ m_filePath = value; }
}
public TaskResults ResultsDisplay
{
get
{ return m_resultsDisplay; }
set
{ m_resultsDisplay = value; }
}
public int NumberDecimals
{
get
{ return m_numberDecimals; }
set
{ m_numberDecimals = value; }
}
}
}
3.在页面上新增TaskResults控件用来显示查询内容ID为TaskResults1,接着在Page_Load事件里添加实例化上面的MapIdentify类具体代码和说明如下:
代码:
//实例化MapIdentify,并且把Map1控件作为参数
MapIdentify identify =new MapIdentify(Map1);
//显示查询结果的控件
identify.ResultsDisplay = TaskResults1;
//小数位数
identify.NumberDecimals =4;
4.接着还需要对RaiseCallbackEvent方法进行修改添加对查询结果处理的代码,具体代码和说明如下:
代码:
//对客户端的请求进行处理
public virtual string RaiseCallbackEvent(string responseString)
{
//对请求字符串进行分割以键值的形式放到NameValueCollection m_queryString中,方便接下来的使用
Array keyValuePairs = responseString.Split("&".ToCharArray())
NameValueCollection m_queryString =new NameValueCollection();
string[] keyValue;
string response ="";
if (keyValuePairs.Length >0)
{
for (int i =0; i < keyValuePairs.Length; i++)
{
keyValue = keyValuePairs.GetValue(i).ToString().Split("=".ToCharArray());
m_queryString.Add(keyValue[0], keyValue[1]);
}
}
else
{
keyValue = responseString.Split("=".ToCharArray());
if (keyValue.Length >0)
{
m_queryString.Add(keyValue[0], keyValue[1]);
}
}
//请求字符串样例:ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication,这样就可以很容易理解了
string controlType = m_queryString["ControlType"];
string eventArg = m_queryString["EventArg"];
if (controlType ==null)
{
controlType ="Map";
}
//根据controlType的不同对请求做不同的处理
switch (controlType)
{
case "Map":
if (eventArg =="CloseOutApplication")//关闭页面请求-CloseOut()
{
//ServerContext对象,需要 ESRI.ArcGIS.Server;
IServerContext context;
for (int i =0; i < Session.Count; i++)
{
//清除session
context = Session as IServerContext;
if (context !=null) {
context.RemoveAll();
context.ReleaseContext();
}
Session.RemoveAll();
//从webconfig中获取关闭后的页面地址
response = ConfigurationManager.AppSettings["CloseOutUrl"];
if (response ==null|| response.Length ==0)
{
response ="ApplicationClosed.aspx";
}
}
}
else if (eventArg =="GetCopyrightText")
//显示版权-webMapAppGetCopyrightText()
{
System.Text.StringBuilder sb =new System.Text.StringBuilder();
//webMapAppGetCopyrightText()请求服务器后返回结果由processCallbackResult进行客户端处理
//关于processCallbackResult方法的参数格式 控件类型:::控件ID:::内容类型:::内容,如div:::mydiv:::content:::你好,GIS!
sb.AppendFormat("///:::{0}:::innercontent:::", "CopyrightTextContents");
int sbLength = sb.Length;
//获取版权内容
sb.Append(GetCopyrightText());
if (sb.Length == sbLength)
{
//没有获取,显示为没有版权
sb.Append("No Copyright information available.");
}
response = sb.ToString();
}
else if (eventArg =="MapIdentify")
//当eventArg == "MapIdentify"是调用identify类的方法进行处理并且返回内容
{
if (identify !=null)
{
identify.Map = Map1;
response = identify.Identify(m_queryString);
}
}
break;
default:
break;
}
//输出给客户端的内容
return response;
}
5.还需要编写客户端的脚本MapIdentify('Map1');响应MapIdentify工具的操作,在javascript目录中新增display_mapidentify.js文件,至于页面对这个文件的引用在前面的MapIdentify类的SetupIdentify()方法已经添加的对这个文件的引用,具体的代码和说明如下:
代码:
//路径
var identifyFilePath ="";
var identifyImageType ="png";
//Tool MapIdentify的ClientAction
function MapIdentify(divid)
{
//获取地图控件
map = Maps[divid];
//esri的方法,把工具条状态切换到MapIdentify的状态
MapPoint(map.controlName, "MapIdentify", false);
//设置在地图上按下鼠标后的事件为MapIdClick
map.divObject.onmousedown = MapIdClick;
}
//鼠标在地图上按下鼠标后触发的事件
function MapIdClick(e)
{
//鼠标指针显示
map.cursor = map.divObject.style.cursor;
//map.divObject.style.cursor = "wait";
getXY(e);
//esri的方法,获取地图控件容器的坐标位置
var box = calcElementPosition(map.containerDivId);
//鼠标相对与地图控件的x坐标
zleft = mouseX - box.left;
//鼠标相对与地图控件的y坐标
ztop = mouseY - box.top;
map.xMin=zleft;
map.yMin=ztop;
//查找IdentifyLocation元素
var div = document.getElementById("IdentifyLocation");
if (div==null) {
//调用在地图上添加小图标方法
addIdentifyLocation();
}
//查找页面上的TaskResults1控件
var fpBody = document.getElementById("Results_TaskResults1");
var html = fpBody.innerHTML;
//进行载入时的信息提醒
fpBody.innerHTML ="<div><img src='images/callbackActivityIndicator.gif' align='middle'/> 正在获取信息. . .</div>"+ html;
//显示放置TaskResults1的Results控件
showFloatingPanel('Results');
pBody=document.getElementById('Results_BodyRow');
//如果Results控件为关闭状态就切换到展可视状态
if (fpBody.style.display=="none")
{
toggleFloatingPanelState('Results','images/collapse.gif','images/expand.gif');
}
//传递给服务器端的参数
var message ="ControlID=Map1&ControlType=Map&EventArg=MapIdentify&Map1_mode=MapIdentify&minx="+ zleft +"&miny="+ ztop;
var context = map.controlName;
//用javascript的eval的方法执行identifyCallbackFunctionString字符串
eval(map.identifyCallbackFunctionString);
//考虑小图标的高和宽,一遍小图标的下尖点刚刚在点击的位置上
var div = document.getElementById("IdentifyLocation");
var cWidth = Math.floor(div.clientWidth /2);
var cHeight = div.clientHeight;
if (cWidth==0) cWidth =12;
if (cHeight==0) cHeight =29;
var idLeft = zleft - parseInt(map.divObject.style.left) - cWidth;
var idTop = ztop - parseInt(map.divObject.style.top) - cHeight +2;
//移动IdentifyLocation的位置,并且显示IdentifyLocation
window.setTimeout('moveLayer("IdentifyLocation", '+ idLeft +', '+ idTop +'); showLayer("IdentifyLocation");', 0);
map.mode = map.tempMode;
map.actionType = map.tempAction;
map.cursor = map.tempCursor;
return false;
}
//在地图上添加小图标
function addIdentifyLocation()
{
var content ='<div id="IdentifyLocation" style="position: absolute; left: 0px; top: 0px; visibility: hidden;">';
//根据浏览器的不同进行相应的处理
if (isIE && ieVersion <7&& (identifyImageType.toLowerCase()=="png"))
{
content +='<img src="'+ identifyFilePath +'images/blank.gif" alt="" border="0" hspace="0" vspace="0" style="filter: progidXImageTransform.Microsoft.AlphaImageLoader(src=/'' + identifyFilePath + 'images/identify-map-icon.png/');" />/n';
}
else
{
content += '<img src="' + identifyFilePath + 'images/identify-map-icon.png" alt="" border="0" hspace="0" vspace="0"/>/n';
}
content +='</div>';
//把包含小图标的div添加到地图之上
map.overlayObject.insertAdjacentHTML("BeforeEnd", content);
}
6.到此为止完成了Identify功能的开发,调试运行查看效果。剩下的下一篇继续写。
这篇关于ArcGIS.Server.9.2.DotNet自带例子分析(一、三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!