超图当前提供的AjaxMap没有提供直接获取Geometry,而我们很多时候需要使用Geometry的坐标点来进行其他分析,在这里有一个解决办法,就是使用TCPMap来实现。
MapQuery.ashx是我们自己创建的一个C# ASP.NET Web Handler File程序文件。代码如下:
<%@ WebHandler Language="C#" Class="MapQuery" %>
using System;
using System.Web;
using System.Xml;
using SuperMap.IS.Web;
using SuperMap.IS.ServiceInterface;
using SuperMap.IS.Utility;
using System.Collections;
public class MapQuery : IHttpHandler {
private IMap m_Map = null;
private string m_strMapName = "";
public void ProcessRequest (HttpContext context) {
string szMethod = HandelRequestString(context.Request["Method"]);
m_strMapName = HandelRequestString(context.Request["Map"]);
if ((szMethod == "") || (m_strMapName == ""))
{
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Write("参数不正确。");
}
else {
string szResult = "";
switch (szMethod) {
case "GetLayerNamesByMapName"://查询指定地图内的图层名
szResult = GetLayerNamesByMapName(m_strMapName);
break;
case "GetMapCoords"://获取图形的坐标点
string szGeoFeature = HandelRequestString(context.Request["GeoParams"]);
szResult = GetMapCoords(szGeoFeature);
break;
}
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Write(szResult);
}
}
public bool IsReusable {
get {
return false;
}
}
/// <summary>
/// 处理传入的参数,去掉无关信息
/// </summary>
/// <param name="str">字符串参数</param>
public string HandelRequestString(String str) {
if (str != null)
{
string szTemp = str.Remove(0, 1);
return szTemp.Remove(szTemp.Length - 1, 1);
}
else {
return null;
}
}
/// <summary>
/// 解析参数,返回参数数组,根据实际需要来实现此函数
/// </summary>
/// <param name="str">字符串参数</param>
public string[] ParseRequestString(String str)
{
ArrayList aryTemp = new ArrayList();
int npos = str.IndexOf(",");
while (npos >= 0) {
string szVal = str.Substring(0, npos);
if (szVal != "") {
aryTemp.Add(szVal);
}
str = str.Remove(0, npos+1);
npos = str.IndexOf(",");
}
if (str != "") {
aryTemp.Add(str);
}
string[] results = new string[aryTemp.Count];
aryTemp.CopyTo(results);
return results;
}
/// <summary>
/// 处理传入的图形查询参数,根据实际需要的编写这部分函数
/// </summary>
/// <param name="str">字符串参数</param>
/// <param name="aryIds"></param>
/// <param name="szlayer">字符串参数</param>
/// <param name="ntype">字符串参数</param>
public bool ParseGeoFeatString(String str, ref int[] nIDs,ref string szlayer)
{
if (str != null)
{
int nPos = str.IndexOf(":");
if (nPos > 0) {
szlayer = str.Substring(0, nPos);
str = str.Remove(0, nPos + 1);
nPos = str.IndexOf(",");
ArrayList aryTemp = new ArrayList();
while (nPos > 0) {
int nId = Convert.ToInt32(str.Substring(0, nPos));
aryTemp.Add(nId);
str = str.Remove(0, nPos + 1);
nPos = aryTemp.IndexOf(",");
}
if (str != "") {
aryTemp.Add(Convert.ToInt32(str));
}
if (aryTemp.Count > 0){
nIDs = new int[aryTemp.Count];
aryTemp.CopyTo(nIDs);
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 查询符合条件的图形元素点集
/// </summary>
/// <param name="szGeoFeature">查询条件</param>
public string GetMapCoords(string szGeoFeature)
{
int[] nSMIDs = new int[0];
string szGeoLayer = "";
string szResult = "";
if (m_Map == null)
{
m_Map = TcpMap.Create("***.***.***.***", 8800, new Hashtable());
}
if (ParseGeoFeatString(szGeoFeature, ref nSMIDs, ref szGeoLayer))
{
Entity[] resultEntities = m_Map.GetEntities(m_strMapName, szGeoLayer, nSMIDs);
MapCoord[] mapCoords = new MapCoord[0];
ArrayList aryTemp = new ArrayList();
if (resultEntities != null)
{
for (int i = 0; i < resultEntities.Length; i++)
{
Geometry geo = resultEntities[i].Shape;
aryTemp.AddRange(geo.Points);
}
mapCoords = new MapCoord[aryTemp.Count];
aryTemp.CopyTo(mapCoords);
}
szResult += mapCoords.Length.ToString() + ":";
for (int i = 0; i < mapCoords.Length; i++) {
szResult += mapCoords[i].X.ToString();
szResult += ",";
szResult += mapCoords[i].Y.ToString();
szResult += ";";
}
}
return szResult;
}
/// <summary>
/// 获取图层名
/// </summary>
/// <param name="szGeoFeature">查询条件</param>
public string GetLayerNamesByMapName(string m_strMapName)
{
string szResult = "";
if (m_Map == null)
{
m_Map = TcpMap.Create("***.***.***.***", 8800, new Hashtable());
}
MapParam mp = new MapParam(); // 定义地图参数
// 定义生成的地图图片的尺寸
mp.Viewer.LeftTop.X = 0;
mp.Viewer.LeftTop.Y = 0;
mp.Viewer.RightBottom.X = 2;
mp.Viewer.RightBottom.Y = 2;
// 设置被浏览地图的名称
mp.MapName = m_strMapName;
// 设置是否返回地图的图层信息
mp.ReturnLayers = true;
try
{
// 根据设置的地图参数,地图对象向地图服务获取符合条件的地图,返回地图以及相关地图信息的描述,包括地图图片的地址
m_Map.GetMapImage(mp);
mp = m_Map.GetCurrentMapParam();
for (int i=0;i<mp.Layers.Length;i++){
szResult += mp.Layers[i].Name;
szResult += ";";
}
}
catch
{
szResult = "";
}
return szResult;
}
}
==============分割线=======================
完成了服务器段代码后,下一步就是实现客户段代码:
/********************************************************************************************************************************/
/************************************ 功能描述:自定义空间分析的异步调用函数 *******************************************/
/************************************ 联系方式:sichen@isoftstone.com *******************************************/
/************************************ 历史信息:2007-1-24创建 *******************************************/
/********************************************************************************************************************************/
function _cscCustomAnalyst(paramNames, paramValues, onComplete, onError){
var xhr=_GetXmlHttpRequest();
xhr.open("post","MapQuery.ashx", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange=function(){
var readyState=xhr.readyState;
if (readyState==4){
var status=xhr.status;
if(status==200){
var resultset = xhr.responseText;
if(resultset == null){
onComplete(null);
return;
}
if(onComplete){
onComplete(resultset);
resultset = null;
}
}
else{
if(onError){
onError(xhr.responseText);
}
}
xhr.onreadystatechange = function(){};
xhr = null;
}
};
var paramString=null;
if(paramNames&¶mNames.length>0){
var params = new Array();
while(paramNames&¶mNames.length>0)
{
params.push(paramNames.shift()+"="+_ToJSON(paramValues.shift()));
}
paramString = params.join("&");
}
xhr.send(paramString);
};
/********************************************************************************************************************************/
/************************************ 功能描述:获取地图的图层列表 *******************************************/
/************************************ 联系方式:sichen@isoftstone.com *******************************************/
/************************************ 历史信息:2007-3-13创建 *******************************************/
/********************************************************************************************************************************/
function GetLayerNames(){
_cscCustomAnalyst(["Method", "Map"], ["GetLayerNamesByMapName", MapName],
initLayerNameList,onQueryError);//initLayerNameList使用返回的地图名字符串来生成一个图层列表
}
/********************************************************************************************************************************/
/************************************ 功能描述:查询省区范围 *******************************************/
/************************************ 联系方式:sichen@isoftstone.com *******************************************/
/************************************ 历史信息:2007-3-13创建 *******************************************/
/********************************************************************************************************************************/
function QueryMapObject(){
_cscCustomAnalyst(["Method", "Map", "GeoParams"], ["GetMapCoords", MapName,"全国省界@" + MapName + ":3"],//GeoParams这个参数是传入图形SMID,图层名。QueryByGeoandSQL这个函数是使用返回的坐标数据的函数,根据实际需要编写。
QueryByGeoandSQL,onQueryError);
}