js脚本:
//图元编辑中的增加点图元
function AddPoint()
{
var map = $find('Map1');
//画点函数
map.getGeometry(ESRI.ADF.Graphics.ShapeType.Point, usePoint, null, 'red', '#0000FF', 'pointer', true);
//获取鼠标点击的位置的地图坐标
function usePoint(clickPoint)
{
//地理坐标转到屏幕坐标
var screenPoint=map.toScreenPoint(clickPoint);
//获取屏幕坐标的X和Y分量
var screenX=screenPoint.offsetX;
var screenY=screenPoint.offsetY;
var message='ActiveType=clickpoint&EventArg=';
message+="States";
message+="&Condtion="+screenX+","+screenY;
var context="Page1";
<%=clickpointCallBack %>
}
}
后台的clickpointCallBack 代码:
//增加点图元
public string clickpointCallBack;
clickpointCallBack = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);
//ICallbackEventHandler先执行RaiseCallbackEvent方法
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
callbackArg = eventArgument;
}
string ICallbackEventHandler.GetCallbackResult()
{
string v = "";
//将传入参数依据&分割符分到querystring变量中
Array keyValuePairs = callbackArg.Split("&".ToCharArray());
NameValueCollection queryString = new NameValueCollection();
string[] keyValue;
if (keyValuePairs.Length > 0)
{
for (int i = 0; i < keyValuePairs.Length; i++)
{
keyValue = keyValuePairs.GetValue(i).ToString().Split("=".ToCharArray());
queryString.Add(keyValue[0], keyValue[1]);
}
}
else
{
keyValue = callbackArg.Split("=".ToCharArray());
if (keyValue.Length > 0)
queryString.Add(keyValue[0], keyValue[1]);
}
//针对参数中指定的ActiveType不同执行不同操作
string controlType = queryString["ActiveType"];
string eventArg = queryString["EventArg"];
string condition = queryString["Condtion"];
switch (controlType)
{
case "clickpoint":
TYBJHelper.AddPoint(condition, Map1);
v = Map1.CallbackResults.ToString();
break;
}
return v;
}
在app_code里面添加TYBJHelper类,在该类中添加AddPoint方法,代码如下:
/// <summary>
/// 添加点图元
/// </summary>
/// <param name="point">点的屏幕坐标(是以屏幕中所画定的地图中的左上角为原点)</param>
/// <param name="map">地图</param>
public static void AddPoint(string point, Map map)
{
int num = point.IndexOf(',');
if (num == -1)
return;
string[] str = point.Split(',');
int centerPointX = int.Parse(str[0]);
int centerPointY = int.Parse(str[1]);
//获得图层的能力
MapFunctionality mf = map.GetFunctionality("Portland") as MapFunctionality;
//获得图层的描述
ESRI.ArcGIS.ADF.ArcGISServer.MapDescription mapDescription = mf.MapDescription;
//把屏幕上的点转换为ADF点
ESRI.ArcGIS.ADF.Web.Geometry.Point adf_map_point = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(centerPointX, centerPointY, map.Extent, mf.DisplaySettings.ImageDescriptor.Width, mf.DisplaySettings.ImageDescriptor.Height);
//定义点对象
PointN ags_map_point = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPoint(adf_map_point);
//设置点的颜色
ESRI.ArcGIS.ADF.ArcGISServer.RgbColor rgb = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
rgb.Red = 255;
rgb.Green = 0;
rgb.Blue = 0;
rgb.AlphaValue = 255;
//设置点的标识符
ESRI.ArcGIS.ADF.ArcGISServer.SimpleMarkerSymbol sms = new ESRI.ArcGIS.ADF.ArcGISServer.SimpleMarkerSymbol();
sms.Style = ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleMarkerStyle.esriSMSDiamond;
sms.Color = rgb;
sms.Size = 10.0;
//设置点
ESRI.ArcGIS.ADF.ArcGISServer.MarkerElement marker = new ESRI.ArcGIS.ADF.ArcGISServer.MarkerElement();
marker.Symbol = sms;
marker.Point = ags_map_point;
if (mapDescription.CustomGraphics != null)
{
//获得该图层上的所有对象
GraphicElement[] oldges = mapDescription.CustomGraphics;
//对象的个数
int cnt = oldges.Length;
//对象个数加一,并把新的对象(点)放进去
GraphicElement[] newges = new GraphicElement[cnt + 1];
oldges.CopyTo(newges, 0);
newges[cnt] = marker;
mapDescription.CustomGraphics = newges;
}
else
{
GraphicElement[] ges = new GraphicElement[1];
ges[0] = marker;
mapDescription.CustomGraphics = ges;
}
object[] oa = new object[1];
//给页面上需要显示的一个div设置样式
string sa = "var griddiv = document.getElementById('hjjcdxx');";
sa += "griddiv.style.display ='block';";
sa += "griddiv.style.position='absolute';";
//left和top需要根据屏幕确定
sa += "griddiv.style.left=" + centerPointX + "+770;";
sa += "griddiv.style.top=" + centerPointY + "+70;";
oa[0] = sa;
CallbackResult cr1 = new CallbackResult(null, null, "javascript", oa);
map.CallbackResults.Add(cr1);
map.Refresh();
}