問(wèn)題描述
我正在嘗試開(kāi)發(fā)自己的增強(qiáng)現(xiàn)實(shí)引擎.
在互聯(lián)網(wǎng)上搜索,我發(fā)現(xiàn)這個(gè)有用的還有另一種獲取beta的實(shí)現(xiàn)方式:
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {浮動(dòng)縱向差異 = second.longitude - first.longitude;float latitudinalDifference = second.latitude - first.latitude;浮動(dòng)可能方位角 = (M_PI * .5f) - atan(latitudinalDifference/longitudinalDifference);if (longitudinalDifference > 0)返回可能的方位角;else if (longitudinalDifference < 0)返回可能的方位角 + M_PI;else if (latitudinalDifference < 0)返回 M_PI;返回 0.0f;}
它使用這個(gè)公式:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference/longitudinalDifference);
為什么 (M_PI * .5f) 在這個(gè)公式中?沒(méi)看懂.
繼續(xù)搜索,我發(fā)現(xiàn)另一個(gè)頁(yè)面在談?wù)撊绾斡?jì)算2個(gè)位置的距離和方位.在此頁(yè)面中還有另一個(gè)實(shí)現(xiàn):
/*** 返回從該點(diǎn)到提供點(diǎn)的(初始)方位角,以度為單位* 見(jiàn) http://williams.best.vwh.net/avform.htm#Crs** @param {LatLon} 點(diǎn):目標(biāo)點(diǎn)的緯度/經(jīng)度* @returns {Number} 初始方位角(以度數(shù)為單位)*/LatLon.prototype.bearingTo = 函數(shù)(點(diǎn)){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);返回 (brng.toDeg()+360) % 360;}
哪個(gè)是正確的?
計(jì)算方位
//來(lái)源JSONObject 源 = step.getJSONObject("start_location");雙 lat1 = Double.parseDouble(source.getString("lat"));雙 lng1 = Double.parseDouble(source.getString("lng"));//目的地JSONObject 目的地 = step.getJSONObject("end_location");雙 lat2 = Double.parseDouble(destination.getString("lat"));雙 lng2 = Double.parseDouble(destination.getString("lng"));雙 dLon = (lng2-lng1);雙 y = Math.sin(dLon) * Math.cos(lat2);雙倍 x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);雙 brng = Math.toDegrees((Math.atan2(y, x)));brng = (360 - ((brng + 360) % 360));
將度數(shù)轉(zhuǎn)換為弧度
弧度 = 度數(shù) * PI/180
將弧度轉(zhuǎn)換為度數(shù)
度數(shù) = 弧度 * 180/PI
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.
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.
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).
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);
Why is (M_PI * .5f) in this formula? I don't understand it.
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?
Calculate bearing
//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));
Convert Degrees into Radians
Radians = Degrees * PI / 180
Convert Radians into Degrees
Degrees = Radians * 180 / PI
這篇關(guān)于計(jì)算兩個(gè)位置之間的方位角(緯度、經(jīng)度)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!