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

标签 javascript google-maps leap-motion

我在 SO 上看到过很多类似的帖子,但到目前为止没有一个对我有帮助。我正在用 JavaScript 做这个。我需要使用 Leap Motion 软件以向量(X,Y,Z)的形式转换在屏幕上拍摄的位置,并将其转换为坐标(纬度)形式的谷歌地图位置,经度)。我正在尝试计算跳跃 Action 手势(将其视为屏幕上的光标)与谷歌地图 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;
}

这是我从另一篇文章中找到的,但我得到的距离总是错误的。例如,如果我在 map 上有两个标记,无论我将跳跃运动光标放在哪里,我的函数都会告诉我最近的一个是标记 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。

关于javascript - 如何将矢量(X,Y)位置转换为纬度/经度坐标? JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27049650/

相关文章:

javascript - 如何获得垂直对齐元素的正确顶部位置?

Android 6.0 应用程序崩溃,谷歌地图上出现错误 null

android - 如何使用 Android Google map 应用程序的双击标记打开新 Activity ?

c++ - LeapMotion、Qt 和信号

javascript - 如何检测圆形手势方向?

javascript - 在页面完全呈现后运行 javascript

javascript - bot.login(token); 怎么样?输入意外结束?

javascript - 如何将每个 JSON 行添加到全局 jQuery 变量中?

android - 无法创建集群管理器实例(使用 Google Maps Android API 实用程序库)

javascript - Leap Motion JS - 后台更新