繁体   English   中英

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

[英]How to convert a vector (X,Y) position to a latitude/longitude coordinate? Javascript

我在SO上看到过很多类似的帖子,但到目前为止,都没有任何帮助。 我正在用Javascript做。 我需要使用Leap Motion软件将屏幕上的位置转换为矢量(X,Y,Z)的形式,并将其转换为坐标(纬度)形式的google maps位置,经度)。 我正在尝试计算跳跃动作手势(将其视为屏幕上的光标)与在google maps api上显示的标记之间的距离。 目前,我的公式是:

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)和经度/纬度之间转换的任何帮助,谢谢!

因此,似乎想要隔离,调试并确保正常工作的第一步是这样:确保对跳跃运动位置坐标转换为屏幕像素坐标的方式感到满意。

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

您是否尝试过此示例? https://developer.leapmotion.com/libraries/screenposition-plugin

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

这样,您可以将像素坐标插入需要屏幕位置的任何API(通常是通过鼠标)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM