这是制作指南针的方法吗?

时间:2011-06-05 17:09:03

标签: iphone objective-c vector location compass-geolocation

我的一个iPhone应用程序的一部分需要显示指向某个位置的指南针。它不是北方 - 它是一个位置(使用纬度和经度)。

我已经提出了下面的解决方案,但是想问你这是否是制作指南针应用程序的最佳方法,如果它可以更精确,是否还有什么我没有考虑过?

我已经制作了这个示例应用程序来向您展示我的代码及其工作原理。 样本上传到http://codeinacup.com/pub/POICompass.tgz

这是源代码的核心:

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];

    // Getting the user's location
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D user = [location coordinate];

    [self calculateAngle:user];
}

- (void)calculateAngle:(CLLocationCoordinate2D)user {
    /*
     We imagine an x,y-coordinate system with the user as center (Q)
     At this point we do not know in which direction the user faces - we assume he faces north.
     The decided location (the Eiffel Tower in Paris for this example) is plotted into the coordinate system as L.
     A third point is made. This is called P. This must be on one of the axis and be perpendicular with the location (L)

     Now we decide if L is in the first (NE), second (SE), third (SW) or fourth (NW) quadrant.
     This is done by comparing the user's latitude (x) and longitude (y) with the latitude (x) and longitude (y) of the location.
     If P is in the first quadrant, we assume that the user is facing north.
     If P is in the second quadrant, we assume that the user is facing east.
     If P is in the third quadrant, we assume that the user is facing south.
     If P is in the fourth quadrant, we assume that the user is facing west.

     Now we can make two vectors: QP (from the user's location (Q) to the point (P)) and QL (from the user's location (Q) to the decided location (L))
     When having two vectors we can calculate the angle (V)

     Now we know the angle between one of the axis to the decided location (L).
     Whenever the iPhone rotates an amount of degrees, we rotate the needle the same amount of degrees in the other direction.

     Check ILLUSTRATION.PNG to see the above illustrated.
    */

    /*
     Q is the location of the user.
     L is the decided location (in this example, the Eiffel Tower)
     P is a point on the axis which is perpendicular with L.
    */

    /*
     (x, y) Coordinates
     x = latitude
     y = longitude
    */

    float locLat = 48.512972;
    float locLon = 2.174017;

    float pLat;
    float pLon;

    if(locLat > user.latitude && locLon > user.longitude) {
        /*
         L is in the first quadrant
         NE
        */

        pLat = user.latitude;
        pLon = locLon;

        degrees = 0;
    }
    else if(locLat > user.latitude && locLon < user.longitude) {
        /*
         L is in the second quadrant
         SE
        */

        pLat = locLat;
        pLon = user.longitude;

        degrees = 45;
    }
    else if(locLat < user.latitude && locLon < user.longitude) {
        /*
         L is in the third quadrant
         SW
        */

        pLat = locLat;
        pLon = user.latitude;

        degrees = 180;
    }
    else if(locLat < user.latitude && locLon > user.longitude) {
        /*
         L is in the fourth quadrant
         NW
        */

        pLat = locLat;
        pLon = user.longitude;

        degrees = 225;
    }
    else {
        NSLog(@"Failed locating.");
    }

    // Vector QP (from user's location to point)
    float vQPlat = pLat - user.latitude;
    float vQPlon = pLon - user.longitude;

    // Vector QL (from user's location to decided location)
    float vQLlat = locLat - user.latitude;
    float vQLlon = locLon - user.longitude;

    // Angle (V) between QP and QL
    float cosDegrees = (vQPlat * vQLlat + vQPlon * vQLlon) / sqrt((vQPlat * vQPlat + vQPlon*vQPlon) * (vQLlat * vQLlat + vQLlon * vQLlon));
    degrees = degrees + acos(cosDegrees);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    /*
     Everytime the user's location updates we need to call - (void)calculateAngle:user to calculate the degrees
     between the user (Q) and the decided location (L)
    */

    [self calculateAngle:newLocation.coordinate];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    /*
     When the phone is rotated, the needle is rotated the same amount of degrees in the other direction
    */

    [needle setTransform:CGAffineTransformMakeRotation((degrees - newHeading.magneticHeading) * M_PI / 180)];
}

1 个答案:

答案 0 :(得分:3)

这不是方法。具体来说,您对两点之间角度的计算是不正确的,因为它忽略了许多关于纬度和经度的事情,并将其视为笛卡尔坐标系。而是使用此very useful article中的公式。另外,不要对基于象限的轴承做出假设,只需使用直接从指南针获得的轴承。