如何将矢量(X,Y)位置转换为纬度/经度坐标?Javascript
How to convert a vector (X,Y) position to a latitude/longitude coordinate? Javascript
我在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。
相关文章:
- 使用地图框 API 将邮政编码转换为纬度和经度
- 将诸如“;纽约水牛城”;使用Google Maps API将地图转换为纬度和经度
- 在 Three.js 中将经度和纬度转换为 xyz(以便旋转相机)时出现问题
- 谷歌地图和GeoJSon:纬度和经度转换
- 经度纬度 (WGS84) 到圆锥变换的转换
- 如何将经度/纬度转换为静态地图图像
- 将数组转换为谷歌纬度/液化数组
- 如何将图像像素转换为纬度和液化天然气中的相同位置以供谷歌地图使用
- 将地址转换为纬度和经度 JS
- 将纬度/纬度转换为像素坐标
- 如何将矢量(X,Y)位置转换为纬度/经度坐标?Javascript
- 将纬度和经度值转换为地址
- 获取纬度&将不同位置的经度值转换为一个值
- 使用谷歌地图API将纬度和经度转换为街道地址
- 在阿尔伯斯投影地图上将纬度/经度转换为X/Y坐标
- 将纬度/经度转换为地址
- 如何在php中转换谷歌地图中的任何纬度和经度地址
- 如何将纬度和经度(从gps方法)转换为几何图形.location谷歌地图对象
- 使用谷歌地图将给定纬度的海里转换为经度
- 如何将经度和纬度转换为街道地址