iPhone指南针GPS方向

Jes*_*siu 3 iphone gps location compass-geolocation

我正在尝试开发一个使用iPhone的GPS和指南针的应用程序,以指向某个特定位置的指针(就像指南针总是指向北方).位置是固定的,无论用户位于何处,我总是需要指针指向该特定位置.我有这个位置的Lat/Long坐标,但不知道如何使用指南针和GPS指向该位置...就像http://www.youtube.com/watch?v=iC0Xn8hY80w此链接1: 20'

我写了一些代码,然而,它无法正确旋转方向.

-(float) angleToRadians:(double) a {
    return ((a/180)*M_PI);
}

-(void)updateArrow {    
    double alon=[longi doubleValue];//source
    double alat=[lati doubleValue];//source
    double blon=[pointlongi doubleValue];//destination
    double blat=[pointlati doubleValue];//destination

    float fLat = [self angleToRadians:alat];
    float fLng = [self angleToRadians:alon];
    float tLat = [self angleToRadians:blat];
    float tLng = [self angleToRadians:blon];

    float temp = atan2(sin(tLng-fLng)*cos(tLat), 
        cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)); 
    double temp2= previousHeading;

    double temp1=temp-[self angleToRadians:temp2];

    /*I using this,but it can't rotate by :point even i change the coordinate
      in CGPointMake */
    Compass2.layer.anchorPoint=CGPointMake(0, 0.5);

    [Compass2 setTransform:CGAffineTransformMakeRotation(temp1)];
    /* Compass2 is a UIImageView like below picture I want to rotate it around 
     : point in image

        ^
        |
        | 
        |
        :
        |
    */
Run Code Online (Sandbox Code Playgroud)

Kei*_*tes 5

你可以使用一个标准的"航向"或"方位"方程 - 如果你在lat1,lon1,你感兴趣的点是lat2,lon2,那么方程是:

heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))
Run Code Online (Sandbox Code Playgroud)

这为您提供弧度方位,您可以通过乘以180 /π转换为度数.然后该值介于-180和180度之间,因此要获得标准罗盘轴承,请将360添加到任何否定答案.

atan2是一个与arctan相关的标准函数,它可以为您的目标点所处的四个可能象限做正确的事情.