计算两个位置(纬度,长)之间的轴承 [英] Calculate bearing between two locations (lat, long)
问题描述
我想开发自己的增强现实引擎。
I'm trying to develop my own augmented reality engine.
在搜索在互联网上,我发现这个有用教程。读它,我看到,重要的是用户的位置,点位置和北部之间的轴承。
Searching on internet, I've found this useful tutorial. Reading it I see that the important thing is bearing between user location, point location and north.
下面的图片是从该教程。
The following picture is from that tutorial.
这之后,我写了一个Objective-C的方法来获得测试版:
Following it, I wrote an Objective-C method to obtain 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;
}
但是,当我尝试它,它不能很好地工作。
But, when I try it, it doesn't work very well.
所以,我检查了iPhone AR工具包,看看它是如何工作(我一直在使用这个工具,但它是如此之大对我来说)。
So, I checked iPhone AR Toolkit, to see how it works (I've been working with this toolkit, but it is so big for me).
和,在<一个href="https://github.com/nielswh/iPhone-AR-Toolkit/blob/master/ARKit/ARGeoCoordinate.m">ARGeoCoordinate.m还有另一种实施如何获得的β
And, in ARGeoCoordinate.m there is another implementation of how to obtain 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;
}
它使用这个公式:
It uses this formula:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
为什么(M_PI * .5f)在这个公式?我不明白。
Why is (M_PI * .5f) in this formula? I don't understand it.
和继续搜索,我发现了另一个页面谈到如何计算距离和轴承的2个地点。在该页面中还有另外一种实现:
And continue searching, I've found another page talking about how to calculate distance and bearing of 2 locations. In this page there is another implementation:
/**
* 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;
}
哪一个是正确的?
Which one is the right one?
推荐答案
轴承计算
//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
这篇关于计算两个位置(纬度,长)之间的轴承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!