我的一个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)];
}

最佳答案

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

关于iphone - 这是制作指南针的方法吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6244461/

10-12 00:20