gpt4 book ai didi

math - 使用两个经度/纬度点获取方向(罗盘)

转载 作者:行者123 更新时间:2023-12-03 12:17:15 27 4
gpt4 key购买 nike

我正在为移动设备开发“指南针”。我有以下几点:

point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target location): Latitude = 50.9246, Longitude = 10.2257
我也有以下信息(来自我的Android手机):
The compass-direction in degree, which bears to the north. 
For example, when I direct my phone to north, I get 0°
如何创建一个“罗盘式”箭头,向我显示该点的方向?
这有数学上的问题吗?
编辑:好的,我找到了一个解决方案,它看起来像这样:
/**
* Params: lat1, long1 => Latitude and Longitude of current point
* lat2, long2 => Latitude and Longitude of target point
*
* headX => x-Value of built-in phone-compass
*
* Returns the degree of a direction from current point to target point
*
*/
function getDegrees(lat1, long1, lat2, long2, headX) {

var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);

lat1 = toRad(lat1);
lat2 = toRad(lat2);

var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = toDeg(Math.atan2(y, x));

// fix negative degrees
if(brng<0) {
brng=360-Math.abs(brng);
}

return brng - headX;
}

最佳答案

忘了说我终于找到了答案。该应用程序将确定运输车辆及其目的地的指南针方向。本质上,是用于获取地球曲率,查找 Angular /罗盘读数,然后将该 Angular 与通用罗盘值进行匹配的奇特数学。当然,您可以只保持罗盘读数并将其作为图像旋转量。请注意,这是对到达终点(汽车站)的车辆方向的平均确定,这意味着它不知道道路在做什么(因此,这可能最适用于飞机或过山车 Derby )。

//example obj data containing lat and lng points
//stop location - the radii end point
endpoint.lat = 44.9631;
endpoint.lng = -93.2492;

//bus location from the southeast - the circle center
startpoint.lat = 44.95517;
startpoint.lng = -93.2427;

function vehicleBearing(endpoint, startpoint) {
endpoint.lat = x1;
endpoint.lng = y1;
startpoint.lat = x2;
startpoint.lng = y2;

var radians = getAtan2((y1 - y2), (x1 - x2));

function getAtan2(y, x) {
return Math.atan2(y, x);
};

var compassReading = radians * (180 / Math.PI);

var coordNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
var coordIndex = Math.round(compassReading / 45);
if (coordIndex < 0) {
coordIndex = coordIndex + 8
};

return coordNames[coordIndex]; // returns the coordinate value
}

即:
车辆轴承(mybus,公交车站)
可能返回“NW”,表示其向西北移动

关于math - 使用两个经度/纬度点获取方向(罗盘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8502795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com