Arcgis Server开发技巧系列 动态刷新经纬度坐标

      在这次的开发项目中遇到了一个新的需求,需要动态在页面上的状态栏显示经纬度坐标,并且要度分秒格式的坐标.真是BT啊.....
      好了,言归正传.大家都知道,要在服务器端计算经纬度,然后传回客户端显示,这个方法是不可取的.那么,最好的办法就是在客户端计算经纬度.那么计算经纬度首先应该获取一些基本的信息.就是当前客户端地图两边的经纬度.
      比如,客户端地图的大小是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
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        }



      客户端JS脚本(10进制转换为度分秒):
      
  1// 初始化参数
  2
  3        // leftX: 左上角X经纬度坐标
  4
  5        // rightX: 右下角X经纬度坐标
  6
  7        // topY: 左上角Y经纬度坐标
  8
  9        // bottomY; 右下角Y经纬度坐标
 10
 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
 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(05);
 38            return params[0+ '°' + degree + '′' + second + '″';
 39        }

 40
 41
 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
 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
 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
 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        }

      当然,如果哪位大侠有更好的方法,欢迎指教,感激不尽..

posted on 2006-10-20 10:09  刘寓  阅读(5444)  评论(7编辑  收藏  举报

导航