开发者

Is this the way to make a compass?

A part of one of my iPhone applications needs to display a compass which points to a location. It's not north - it's a location (using latitude and longitude).

I have come up with the solution below but would like to ask you if this is the best way to make a compass application, if it can be more precise and if there are anything that I have not taken into account?

I have made this sample application to show you my code and how it works. The sample is uploaded at http://codeinacup.com/pub/POICompass.tgz

This is the central in the source code:

- (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) * (vQLla开发者_开发问答t * 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)];
}


This is NOT the way. Specifically, your calculation of the angle between the two points is incorrect, as it neglects many things about latitude and longitude, and treats it as a Cartesian coordinate system. Instead use the formulas in this very useful article. Also, don't make assumptions about bearing based on quadrants, just use the bearing you get from the compass directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜