如何将矢量(X,Y)位置转换为纬度/经度坐标? Javascript

问题描述:

我看过很多类似SO的帖子,但目前为止还没有人帮助过我。我正在用Javascript做这个。我需要使用LeapMotion软件以矢量(X,Y,Z)的形式将屏幕上的位置转换为一个位置,并将其转换为一个以坐标(纬度,经度)。我正在尝试计算闰年动作手势(将其视为屏幕上的光标)与标记显示在Google Maps API上的距离。目前我的公式是:如何将矢量(X,Y)位置转换为纬度/经度坐标? Javascript

function convertToLatLng(x, y){ 
    // retrieve the lat lng for the far extremities of the (visible) map 
    var latLngBounds = map.getBounds(); 
    var neBound = latLngBounds.getNorthEast(); 
    var swBound = latLngBounds.getSouthWest(); 

    // convert the bounds in pixels 
    var neBoundInPx = map.getProjection().fromLatLngToPoint(neBound); 
    var swBoundInPx = map.getProjection().fromLatLngToPoint(swBound); 

    // compute the percent of x and y coordinates related to the div containing the map; in my case the screen 
    var procX = x/window.innerWidth; 
    var procY = y/window.innerHeight; 

    // compute new coordinates in pixels for lat and lng; 
    // for lng : subtract from the right edge of the container the left edge, 
    // multiply it by the percentage where the x coordinate was on the screen 
    // related to the container in which the map is placed and add back the left boundary 
    // you should now have the Lng coordinate in pixels 
    // do the same for lat 
    var newLngInPx = (neBoundInPx.x - swBoundInPx.x) * procX + swBoundInPx.x; 
    var newLatInPx = (swBoundInPx.y - neBoundInPx.y) * procY + neBoundInPx.y; 

    // convert from google point in lat lng and have fun :) 
    var newLatLng = map.getProjection().fromPointToLatLng(new google.maps.Point(newLngInPx, newLatInPx)); 

    return newLatLng; 
} 

我发现从另一个SO帖子,但我得到的距离总是错误的。例如,如果我在地图上有两个标记,无论我在哪里放置闰秒运动光标,我的函数都会告诉我最接近的是标记B.

任何帮助将矢量(X,Y )和拉特/长将不胜感激,谢谢!

+0

你是如何得到距离?我们如何重现这个问题?请提供[最小,完整,测试和可读的示例](http://stackoverflow.com/help/mcve)。 – geocodezip 2014-11-20 21:32:12

+0

我试图提供较少的信息,以防人们不熟悉Leap Motion。我还在很多地方打印了控制台消息,这些消息使我相信我所得到的所有信息都是准确的,除非进行计算(即转换数据)时,某些事情是不准确的。但是如果你熟悉Leap Motion,那么我得到的距离是keyTapGesture.position [0](对于X,对于Y是[1])。然后,我试图将这些距离转换为经度和纬度。 – user3727351 2014-11-21 18:20:29

因此,它似乎是你想隔离,调试,并确保工作的第一步是这样的:确保你对快速移动位置坐标转换为屏幕像素坐标的方式感到满意。

这在2d中可能有点棘手,因为通常我们会丢弃z维,然后引入线性比例因子将mm转换为px。

你试过这个例子吗? https://developer.leapmotion.com/libraries/screenposition-plugin

的源代码:https://github.com/leapmotion/leapjs-plugins/tree/master/main/screen-position

有了这一点,你可以插入你的像素坐标需要(通常鼠标)屏幕位置的任何API。

+0

我看了一下这个例子,我对它们是如何从跳跃位置坐标到屏幕像素坐标有点困惑。我想我会看到它使用的基准比例为6,基准偏移为-100。这些数字来自哪里?对不起,我还是这个新手。感谢您的帮助,虽然 – user3727351 2014-11-21 18:57:47

+0

这些数字是随意的 - 对于Leap来说,使用您的应用感觉很好。 – 2014-11-21 22:24:09

+0

例如,x坐标的公式是:(window.innerWidth/2)+(hand.palmPosition [0] * scale)'。这将使飞跃的中间与窗口中间对齐 – 2014-11-21 22:25:52