【问题标题】:Calculate degrees for position计算位置的度数
【发布时间】:2012-05-25 10:41:19
【问题描述】:

我正在构建一个应显示指向某个位置的箭头的应用程序。例如,如果我在位置 X 并且目标设置为位置 Y,它应该显示一个直接指向该位置的箭头。

在搜索和搜索之后,我发现有一些计算这个的花哨的公式,但是我似乎一遍又一遍地弄错了。

这是我的问题。虽然它似乎找到了正确的初始位置,但只要我转动我的设备,“箭头”就会转向相反的方向或它应该转向的方向。所以,如果我顺时针转动我的设备,箭头也会顺时针转动……反之亦然。

这是我的一些代码:

@implementation CompassViewController

BOOL firstPositionFound = NO;
float lastPosition = 0;
float currentHeading = 0;

[...]

#pragma mark - 
#pragma mark Core Location Methods

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    if (newHeading.headingAccuracy > 0) {
        CLLocationDirection theHeading = newHeading.magneticHeading;

        currentHeading = theHeading;

        [self fixPointer:theHeading];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    self.currentLocation = newLocation;

    [self fixPointer:currentHeading];
}

- (void)fixPointer:(float)heading
{
    float degree = [self calculateDegree:self.currentLocation];

    degree = heading - degree;

    NSLog(@"heading: %f, Degree: %f", heading, degree);

    NSLog(@"Degree 2: %f", degree);

    self.arrowView.transform = CGAffineTransformMakeRotation(degreesToRadians(degree));
}

#pragma mark -
#pragma mark Delegate methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Other methods

- (float)calculateDegree:(CLLocation *)newLocation
{
    CLLocationCoordinate2D from = newLocation.coordinate;
    CLLocationCoordinate2D to;
    to.latitude = APP_DESTINATION_LAT;
    to.longitude = APP_DESTINATION_LON;

    float res = atan2(sin((to.longitude*M_PI/180)-(from.longitude*M_PI/180))*cos(to.latitude*M_PI/180),
                      cos(from.latitude*M_PI/180)*sin(to.latitude*M_PI/180)-sin(from.latitude*M_PI/180)*cos(to.latitude*M_PI/180)*cos((to.longitude*M_PI/180)-(from.longitude*M_PI/180)));

    res = res/M_PI*180;

    if (res < 0)
        res = 360.0f + res;

    return res;
}

#pragma mark -

我只是迷路了,有人可以指出我哪里出错了吗?我想它是一些简单的事情,我目前正在盲目地看到。

【问题讨论】:

    标签: ios math gps core-location direction


    【解决方案1】:

    看看这个 Stack Overflow 问题的答案:

    CLLocation Category for Calculating Bearing w/ Haversine function

    具体来说,这部分:

    如果您得到负方位角,请将 2*M_PI 添加到最终结果的弧度方位角(如果在转换为度数后添加,则为 360)。 atan2 返回 -M_PI 到 M_PI(-180 到 180 度)范围内的结果,因此您可能希望使用类似于以下代码的内容将其转换为罗盘方位

    if(radiansBearing < 0.0)
        radiansBearing += 2*M_PI;**
    

    此外,航向信息需要针对设备方向和角度进行调整,除非您始终以纵向方式握持设备。慢慢将您的设备转至横向模式,您会看到您的航向值变化了 90°。

    【讨论】:

    • 谢谢!你的回答确实帮助了我。我将我的代码更新为您引用的代码,这要好得多。但是,我现在仍然有一个问题:似乎我的经度/纬度朝向目的地的度数正确......但它希望手机的航向为 0(指向北方)。如果我计算半径减去方位的航向,它仍然不准确。有什么建议吗?
    • @jenni-b 你对我有什么建议吗?
    • @Paul Peelen 没有看到您的代码,我不知道如何回答您,但是,简单的答案是确保您使用正确的单位并调整设备方向。因此,请确保您从度数中减去度数,而不是从弧度中减去度数,并确保您从磁力计获得的值已针对设备方向进行了调整。
    【解决方案2】:

    看看这个 Wikipedia - On rotation matrix

    我从代码中了解到,您有给定 (x,y) 坐标的 sin 元素和 cos 元素之间的角度。 [基本上由(tan(x,0)/(0,y))给出]

    我们称这个角度为 theta。

    你应该做的是把这个坐标乘以

    让我们调用新坐标 (x',y')

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多