gpt4 book ai didi

android - 计算两个位置之间的方位角(纬度、经度)

转载 作者:IT老高 更新时间:2023-10-28 23:18:30 32 4
gpt4 key购买 nike

我正在尝试开发自己的增强现实引擎。

在互联网上搜索,我发现这很有用 tutorial .阅读它,我发现重要的是用户位置、点位置和北方之间的方位。

以下图片来自该教程。

enter image description here

接着,我写了一个Objective-C方法来获取beta:

+ (float) calculateBetaFrom:(CLLocationCoordinate2D)user to:(CLLocationCoordinate2D)destination
{
double beta = 0;
double a, b = 0;

a = destination.latitude - user.latitude;
b = destination.longitude - user.longitude;

beta = atan2(a, b) * 180.0 / M_PI;
if (beta < 0.0)
beta += 360.0;
else if (beta > 360.0)
beta -= 360;

return beta;
}

但是,当我尝试它时,效果并不好。

所以,我检查了 iPhone AR Toolkit,看看它是如何工作的(我一直在使用这个工具包,但它对我来说太大了)。

而且,在 ARGeoCoordinate.m还有另一种获取beta的实现方式:

- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {

float longitudinalDifference = second.longitude - first.longitude;
float latitudinalDifference = second.latitude - first.latitude;
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);

if (longitudinalDifference > 0)
return possibleAzimuth;
else if (longitudinalDifference < 0)
return possibleAzimuth + M_PI;
else if (latitudinalDifference < 0)
return M_PI;

return 0.0f;
}

它使用这个公式:

float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);

为什么 (M_PI * .5f) 在这个公式中?没看懂。

继续搜索,我找到了另一个 page谈论如何计算2个位置的距离和方位。在此页面中还有另一个实现:

/**
* Returns the (initial) bearing from this point to the supplied point, in degrees
* see http://williams.best.vwh.net/avform.htm#Crs
*
* @param {LatLon} point: Latitude/longitude of destination point
* @returns {Number} Initial bearing in degrees from North
*/
LatLon.prototype.bearingTo = function(point) {
var lat1 = this._lat.toRad(), lat2 = point._lat.toRad();
var dLon = (point._lon-this._lon).toRad();

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 = Math.atan2(y, x);

return (brng.toDeg()+360) % 360;
}

哪个是正确的?

最佳答案

计算方位

//Source
JSONObject source = step.getJSONObject("start_location");
double lat1 = Double.parseDouble(source.getString("lat"));
double lng1 = Double.parseDouble(source.getString("lng"));

// destination
JSONObject destination = step.getJSONObject("end_location");
double lat2 = Double.parseDouble(destination.getString("lat"));
double lng2 = Double.parseDouble(destination.getString("lng"));

double dLon = (lng2-lng1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
double brng = Math.toDegrees((Math.atan2(y, x)));
brng = (360 - ((brng + 360) % 360));

将度数转换为弧度

Radians = Degrees * PI / 180

将弧度转换为度数

Degrees = Radians * 180 / PI

关于android - 计算两个位置之间的方位角(纬度、经度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8123049/

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