Van*_*nel 17 iphone android geolocation augmented-reality
我正在尝试开发自己的增强现实引擎.
在互联网上搜索,我发现了这个有用的教程.阅读它我发现重要的是在用户位置,点位置和北方之间.
以下图片来自该教程.

接下来,我写了一个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;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试它时,它不能很好地工作.
所以,我检查了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;
}
Run Code Online (Sandbox Code Playgroud)
它使用这个公式:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
Run Code Online (Sandbox Code Playgroud)
为什么(M_PI*.5f)在这个公式中?我不明白.
继续搜索,我发现了另一个页面,讨论如何计算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;
}
Run Code Online (Sandbox Code Playgroud)
哪一个是正确的?
Kir*_*ela 19
计算方位
//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));
Run Code Online (Sandbox Code Playgroud)
将度数转换为弧度
Radians = Degrees * PI / 180
Run Code Online (Sandbox Code Playgroud)
将弧度转换为度数
Degrees = Radians * 180 / PI
Run Code Online (Sandbox Code Playgroud)
试试这个以获得准确的结果:
private static double degreeToRadians(double latLong) {
return (Math.PI * latLong / 180.0);
}
private static double radiansToDegree(double latLong) {
return (latLong * 180.0 / Math.PI);
}
public static double getBearing() {
//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 fLat = degreeToRadians(lat1);
double fLong = degreeToRadians(lng1);
double tLat = degreeToRadians(lat2);
double tLong = degreeToRadians(lng2);
double dLon = (tLong - fLong);
double degree = radiansToDegree(Math.atan2(sin(dLon) * cos(tLat),
cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(dLon)));
if (degree >= 0) {
return degree;
} else {
return 360 + degree;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在http://www.sunearthtools.com/tools/distance.php上测试方位结果。
在公式
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
该术语(M_PI * .5f)表示 π/2,即 90°。这意味着它与您最初陈述的公式相同,因为关于上图,它成立
β = 反正切 (a/b) = 90° - 反正切 (b/a)。
因此,如果a指的是经度b差异和纬度差异,则这两个公式是相似的。最后一个公式使用公式的第一部分再次计算相同。
| 归档时间: |
|
| 查看次数: |
25353 次 |
| 最近记录: |