【发布时间】:2009-05-07 06:26:04
【问题描述】:
在开放图层库中,以下是用于将屏幕坐标转换为经纬度的方法。我无法弄清楚这个方法封装的逻辑?
有人可以指点一下吗?
【问题讨论】:
标签: gis latitude-longitude openlayers
在开放图层库中,以下是用于将屏幕坐标转换为经纬度的方法。我无法弄清楚这个方法封装的逻辑?
有人可以指点一下吗?
【问题讨论】:
标签: gis latitude-longitude openlayers
函数根据当前地图的分辨率,以及当前地图中心点的经纬度,以及所选点到地图中心的距离,计算出指定点的经纬度地图。
var center = this.getCenter(); //地图中心纬度/经度 var res = this.getResolution(); //由用户预先定义。代表经纬度的变化... var size = this.getSize();上面的代码收集了计算所需的信息:当前地图视图的中心点(这将为我们提供中心点的纬度/经度)、当前地图分辨率和当前地图大小用户屏幕(可能受屏幕尺寸等影响)。
然后计算如下:
//这是必须在其中显示地图的div的宽度和高度 var delta_x = point.x - (size.w / 2); var delta_y = point.y - (size.h / 2);首先取 x 坐标(以像素为单位)并减去地图的宽度(以像素为单位)。这给了我们一个新的 x 坐标,其中 0 是地图的中心像素。 delta-x 现在应该是范围从 -(size.w/2) 到 +(size.w/2) 的像素值。 然后我们对 y 坐标做同样的事情。 所以 delta-x 和 delta-y 现在是笛卡尔坐标,原点位于地图的中心。
返回新的 OpenLayers.LatLon( center.lat - delta_y * res, center.lon + delta_x * res );我们需要将 delta-x 和 delta-y 从像素转换为纬度/经度。首先,我们将 delta-x 和 delta-y 乘以当前分辨率。这给了我们正确的比例,但不是正确的原点。添加 center.lat 和 center.lon adusts 以根据当前显示的地图为我们提供纬度/经度。
最后,'new OpenLayers.LatLon' 调用只是将上述计算包装在一个 LatLon 对象中,以便它可以作为 LatLon 对象从函数中返回。
编辑:使用像素时,x 坐标的增加通常意味着“向右移动”,而 y 坐标的增加通常意味着“向上移动”。在地图上,当您增加经度时,您通常会“向右移动”。然而,纬度是颠倒的;当您增加纬度时,您通常会在地图上“向下移动”。
因此,Latitude 的工作方向与屏幕上的正常 y 坐标方案相反。因此,在最终计算中,center.lat 使用减号,center.lon 使用加号。
【讨论】:
我重新排列了现有的 cmets,添加了更多内容并添加了一些空白。希望您会发现这一点更清楚。
getLatLonFromPoint: function (point) {
// point is the x and y screen coordinate
// map center lat/lon
var center = this.getCenter();
// pre defined by the user. Represents the change in lat long per screen unit at the given zoom level
var res = this.getResolution();
// this is the width and height of the screen (div) in which the map has to be displayed
var size = this.getSize();
// this is the distance of the point from the center of the screen (div)
var delta_x = point.x - (size.w / 2);
var delta_y = point.y - (size.h / 2);
// return the latitude and longitude
// these are calculated from the center lat/lon using the
// screen distances which are scaled (multiplied) by the resolution
return new OpenLayers.LatLon(
center.lat - delta_y * res,
center.lon + delta_x * res );
}
【讨论】:
试试这个:
map.events.register("mousemove", map, function(e) {
var position = this.events.getMousePosition(e);
var p = map.getLonLatFromPixel(new OpenLayers.Pixel(position.x, position.y));
// your longitude value = p.lon;
// your latitude value = p.lat;
});
【讨论】: