Arcgis Server开发技巧系列 动态刷新经纬度坐标
在这次的开发项目中遇到了一个新的需求,需要动态在页面上的状态栏显示经纬度坐标,并且要度分秒格式的坐标.真是BT啊.....
好了,言归正传.大家都知道,要在服务器端计算经纬度,然后传回客户端显示,这个方法是不可取的.那么,最好的办法就是在客户端计算经纬度.那么计算经纬度首先应该获取一些基本的信息.就是当前客户端地图两边的经纬度.
比如,客户端地图的大小是800*600.我们首先计算0,0这个点的经纬度,然后计算800,600这个点的经纬度.最后,经过一些运算,就可以获得x,y这个点的经纬度了.
当然,这里还有一些需要注意的地方:
一:由于从Arcgis Server中获取的经纬度是10进制的,所以我们还需要经过一些换算才能得到度分秒式的经纬度.
二:我们取经纬度的时候,一定要取MapDesription.Extent的XMIN等信息,而不能直接取屏幕坐标为0,0的信息,不然数据会不正确.具体为什么,我现在还没有弄清楚.
三:每次改变地图的比例尺的时候,一定要重新获取经纬度信息.
客户端JS脚本(10进制转换为度分秒):
当然,如果哪位大侠有更好的方法,欢迎指教,感激不尽..
好了,言归正传.大家都知道,要在服务器端计算经纬度,然后传回客户端显示,这个方法是不可取的.那么,最好的办法就是在客户端计算经纬度.那么计算经纬度首先应该获取一些基本的信息.就是当前客户端地图两边的经纬度.
比如,客户端地图的大小是800*600.我们首先计算0,0这个点的经纬度,然后计算800,600这个点的经纬度.最后,经过一些运算,就可以获得x,y这个点的经纬度了.
当然,这里还有一些需要注意的地方:
一:由于从Arcgis Server中获取的经纬度是10进制的,所以我们还需要经过一些换算才能得到度分秒式的经纬度.
二:我们取经纬度的时候,一定要取MapDesription.Extent的XMIN等信息,而不能直接取屏幕坐标为0,0的信息,不然数据会不正确.具体为什么,我现在还没有弄清楚.
三:每次改变地图的比例尺的时候,一定要重新获取经纬度信息.
1
/// <summary>
2
/// 获取当前Extent的边界空间坐标信息
3
/// </summary>
4
/// <returns>
5
/// double[0]: 该Extent的左上角X坐标
6
/// double[1]: 该Extent的右下角X坐标
7
/// double[2]: 该Extent的左上角Y坐标
8
/// double[3]: 该Extent的右下角Y坐标
9
/// </returns>
10
public double[] GetCurrentGeopraphicCoordinate()
11
{
12
using (WebObject webObj = new WebObject())
13
{
14
IServerContext ctx;
15
ctx = MapManager.CreateServerContext
16
(MapManager.stat_cfgBase.MapConfig.MapServer, "MapServer");
17
if (MapManager.stat_som.GetConfigurationInfo(MapManager.stat_cfgBase.MapConfig.MapServer, "MapServer").IsPooled)
18
{
19
webObj.ManageLifetime(ctx);
20
}
21
IMapServer map = ctx.ServerObject as IMapServer;
22
IMapServerInfo mapinfo = map.GetServerInfo(map.DefaultMapName);
23
IMapDescription md;
24
md = extenthistory != null && extenthistory.Count != 0
25
? ctx.LoadObject(extenthistory[Convert.ToInt32(extentindex[0].ToString())].ToString()) as IMapDescription
26
: mapinfo.DefaultMapDescription;
27
webObj.ManageLifetime(md);
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
double[] coords = new double[]{
30
md.MapArea.Extent.XMin, md.MapArea.Extent.XMax,
31
md.MapArea.Extent.YMin, md.MapArea.Extent.YMax
32
};
33
return coords;
34
}
35
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
客户端JS脚本(10进制转换为度分秒):
1
// 初始化参数
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
// leftX: 左上角X经纬度坐标
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
// rightX: 右下角X经纬度坐标
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
// topY: 左上角Y经纬度坐标
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
// bottomY; 右下角Y经纬度坐标
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
// mapWidth: 地图宽度
12
// mapHeigth: 地图高度
13
function InitInfos(leftX, rightX, topY, bottomY, mapWidth, mapHeight)
14
{
15
m_leftX = leftX;
16
m_rightX = rightX;
17
m_topY = topY;
18
m_bottomY = bottomY;
19
m_mapHeight = mapHeight;
20
m_mapWidth = mapWidth;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
// 将10进制的经纬度坐标转换为度分秒的经纬度坐标
24
// Coordinate:10进制的经纬度
25
// eg:
26
// Coordinate : 104.342050792709
27
// 转换后 : 104°20′32.38″
28
function ConvertToGeopraphicCoordinate (Coordinate)
29
{
30
var params = Coordinate.toString().split('.');
31
var coord = "0." + params[1];
32
var degree = (coord * 60).toString();
33
var coords = degree.split('.');
34
degree = coords[0];
35
var second = "0." + coords[1];
36
second = (second * 60).toString();
37
second = second.substring(0, 5);
38
return params[0] + '°' + degree + '′' + second + '″';
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
// 计算当前的经纬度坐标,并按照度分秒的方式返回
43
// currentX: 当前地图屏幕X坐标
44
// currentY: 当前地图屏幕Y坐标
45
function ComputeCurrentCoordinate(currentX, currentY)
46
{
47
// 获取经纬度10进制的小数部分,用于计算
48
var nonhead_rightX = '0.' + m_rightX.split('.')[1];//0.1339024;//
49
var nonhead_leftX = '0.' + m_leftX.split('.')[1];//0.0550135;//
50
var nonhead_topY = '0.' + m_topY.split('.')[1];
51
var nonhead_bottomY = '0.' + m_bottomY.split('.')[1];
52
53
// 获取经纬度10进制的整数部分,用于最后的合并
54
var head_rightX = m_rightX.split('.')[0];
55
var head_leftX = m_leftX.split('.')[0];
56
var head_topY = m_topY.split('.')[0]; // YMin
57
var head_bottomY = m_bottomY.split('.')[0]; // YMax
58
59
// 提高精确度,减小误差
60
nonhead_rightX = nonhead_rightX * 10000;
61
nonhead_leftX = nonhead_leftX * 10000;
62
nonhead_topY = nonhead_topY * 10000;
63
nonhead_bottomY = nonhead_bottomY * 10000;
64
65
var bRightHead = true;
66
var bTopHead = true;
67
68
// 计算X轴单位值
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
var offset_X = nonhead_rightX - nonhead_leftX;
71
72
if (offset_X < 0)
73
{
74
offset_X = 1 + offset_X;
75
bRightHead = false;
76
}
77
78
// 计算Y轴单位值
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
var offset_Y = nonhead_bottomY - nonhead_topY;
81
if (offset_Y < 0)
82
{
83
offset_Y = 1 + offset_Y;
84
bTopHead = false;
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
// 计算当前经纬度10进制坐标
88
var GeopraphicX = (offset_X/m_mapWidth) * currentX + (nonhead_leftX - 0);
89
var GeopraphicY = (nonhead_bottomY - 0) - (offset_Y/m_mapHeight) * (currentY);// + (nonhead_topY - 0);
90
91
GeopraphicX = GeopraphicX / 10000;
92
GeopraphicY = GeopraphicY / 10000;
93
94
if (bRightHead = true)
95
{
96
GeopraphicX = parseInt(head_rightX) + parseFloat(GeopraphicX);
97
}
98
else
99
{
100
GeopraphicX = parseInt(head_leftX) + parseFloat(GeopraphicX);
101
}
102
103
if (bTopHead = true)
104
{
105
GeopraphicY = parseInt(head_bottomY) + parseFloat(GeopraphicY);
106
}
107
else
108
{
109
GeopraphicY = parseInt(head_topY) + parseFloat(GeopraphicY);
110
}
111
112
var ConvertedX = ConvertToGeopraphicCoordinate(GeopraphicX);
113
var ConvertedY = ConvertToGeopraphicCoordinate(GeopraphicY);
114
115
return ConvertedX + 'E; ' + ConvertedY + 'N';
116
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
当然,如果哪位大侠有更好的方法,欢迎指教,感激不尽..