在开放图层库中,下面是用于将屏幕坐标转换为纬度和经度的方法。我无法弄清楚这种方法封装的逻辑吗?

<代码>

<代码>
getLatLonFromPoint: function (point) {
    var center = this.getCenter();
    //map center lat/lon
    var res  = this.getResolution(); 
    //pre defined by the user. Represents the change in lat long per screen unit at the given zoom level
    var size = this.getSize(); 
    //this is the width and height of the div in which the map has to be displayed
    var delta_x = point.x - (size.w / 2);
    var delta_y = point.y - (size.h / 2);
    return new OpenLayers.LatLon(
        center.lat - delta_y * res,
        center.lon + delta_x * res );
   }

<代码>

有人可以提供一些指示吗?

有帮助吗?

解决方案

该函数根据地图的当前分辨率,当前地图中心点的纬度和经度,以及所选点距离地图中心的距离,计算指定点的纬度和经度。地图。

var center = this.getCenter();
//map center lat/lon
var res  = this.getResolution(); 
//pre defined by the user. Represents the change in lat long ...
var size = this.getSize(); 

上面的代码集合收集计算所需的信息:当前地图视图的中心点(它将给出中心点上的纬度/经度),当前地图分辨率和当前地图大小用户屏幕(可能受屏幕尺寸等影响)。

然后计算如下:

    //this is the width and height of the div in which the map has to be displayed
    var delta_x = point.x - (size.w / 2);
    var delta_y = point.y - (size.h / 2);

首先获取x co-ord(以像素为单位)并减去地图的宽度(以像素为单位)。这给了我们一个新的x co-ord,其中0是地图的中心像素。 delta-x现在应该是一个像素值,范围从 - (size.w / 2)到+(size.w / 2)。 然后我们为同事做同样的事情。 所以delta-x和delta-y现在是笛卡尔坐标,其原点位于地图的中心。

    return new OpenLayers.LatLon(
        center.lat - delta_y * res,
        center.lon + delta_x * res );

我们需要将delta-x和delta-y从像素转换为lat / lon。首先,我们将delta-x和delta-y乘以当前分辨率。这给了我们正确的比例但不是正确的原点。添加centre.lat和centre.lon adusts根据当前显示的地图给我们lat / lon。

最后,'new OpenLayers.LatLon'调用只是在LatLon对象中包含上述计算,因此它可以作为LatLon对象从函数返回。

编辑:处理像素时,x co-ord的增加通常意味着'向右移动',而y co-ord的增加通常意味着'向上移动'。在地图上,当您增加经度时,通常会“向右移动”。然而,纬度是颠倒的;当你增加纬度时,你通常会在地图上“向下移动”。

因此,Latitude在与屏幕上的正常y协调方案相反的方向上工作。因此,在最终计算中,对于centre.lat使用负数,但对于centre.lon则使用加号。

其他提示

我重新安排了现有的评论,添加了一些评论并添加了一些空格。希望你会发现这个更清楚。

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;
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top