我正在嘗試開發自己的增強現實引擎。計算兩個位置之間的軸承(lat,long)
在網上搜索,我發現這個有用的tutorial。閱讀它我發現重要的是在用戶位置,點位置和北部之間。
以下圖片來自該教程。
它之後,我寫了一個Objective-C的方法來獲得測試:
+ (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存在如何獲得測試另一種實現方式:這個公式(.5f M_PI *)在
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference/longitudinalDifference);
爲什麼:
- (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;
}
它使用這個公式?我不明白。
並繼續搜索,我發現另一個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;
}
哪一個是正確的?
你曾經解決過這個問題嗎?我對你使用哪種解決方案感興趣 – Craigy
是的,我不得不短時間添加自己的答案。 – VansFannel