2

iPhone アプリケーションの 1 つの一部で、場所を指すコンパスを表示する必要があります。北ではありません - 場所です (緯度と経度を使用)。

以下の解決策を思いつきましたが、これがコンパス アプリケーションを作成する最良の方法であるかどうか、より正確にできるかどうか、考慮していない点があるかどうかを尋ねたいと思います。

このサンプル アプリケーションを作成して、私のコードとその動作を示します。サンプルは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)];
}
4

1 に答える 1

3

これは方法ではありません。具体的には、緯度と経度に関する多くのことを無視し、デカルト座標系として扱うため、2 点間の角度の計算は正しくありません。代わりに、この非常に役立つ記事の式を使用してください。また、象限に基づいて方位を推測するのではなく、コンパスから直接取得した方位を使用してください。

于 2011-08-05T14:02:18.150 に答える