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

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

本文关键字:纬度 转换 经度 Javascript 坐标 位置      更新时间:2023-09-26

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