计算两个位置之间的方位(纬度,经度) [英] Calculate bearing between two locations (lat, long)

查看:50
本文介绍了计算两个位置之间的方位(纬度,经度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在互联网上搜索,我发现这个很有用的还有另一种获取测试版的实现:

- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {浮动纵向差异 = 第二个经度 - 第一个经度;浮动纬度差异 = 第二个纬度 - 第一个纬度;float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference/VerticalDifference);如果(纵向差 > 0)返回可能的方位角;else if (longitudinalDifference <0)返回可能的方位角 + M_PI;否则如果(纬度差<0)返回 M_PI;返回0.0f;}

它使用这个公式:

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

为什么在这个公式中是 (M_PI * .5f)?我不明白.

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

/*** 返回从该点到提供点的(初始)方位角,以度为单位* 见 http://williams.best.vwh.net/avform.htm#Crs** @param {LatLon} 点:目的地的纬度/经度* @returns {Number} 以北为单位的初始方位角*/LatLon.prototype.bearingTo = 函数(点){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;}

哪一个是正确的?

解决方案

计算方位

//来源JSONObject source = step.getJSONObject("start_location");double lat1 = Double.parseDouble(source.getString("lat"));double lng1 = Double.parseDouble(source.getString("lng"));//目的地JSONObject destination = step.getJSONObject("end_location");double lat2 = Double.parseDouble(destination.getString("lat"));double lng2 = Double.parseDouble(destination.getString("lng"));双 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));

将度数转换为弧度

弧度 = 度数 * PI/180

将弧度转换成度数

度数 = 弧度 * 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

这篇关于计算两个位置之间的方位(纬度,经度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆