使用指南针指向位置

时间:2011-02-23 15:17:44

标签: iphone sdk location compass-geolocation heading

我正在尝试为应用程序开发一个指南针,它在地图上有一组注释。我希望能够选择一个注释,然后让指南针将用户指向所选位置。

我已经计算了从用户位置到注释位置的degress,我有了磁性标题和iPhone的真实标题。我也知道如何旋转图像。 但我无法弄清楚下一步是什么。

用户位置和注释位置之间的度数计算如下:

    // get user location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    float x1 = coordinate.latitude;
    float y1 = coordinate.longitude;
    float x2 = [annLatitude floatValue];
    float y2 = [annLongitude floatValue];

    float dx = (x2 - x1);
    float dy = (y2 - y1);

    if (dx == 0) {
        if (dy > 0) {
            result = 90;
        }
        else {
            result = 270;
        }
    }
    else {
        result = (atan(dy/dx)) * 180 / M_PI;
    }

    if (dx < 0) {
        result = result + 180;
    }

    if (result < 0) {
        result = result + 360;
    }

真实和磁性标题的检索方式如下:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    arrow.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading);

    // NSLog(@"magnetic heading: %f", newHeading.magneticHeading);
    // NSLog(@"true heading: %f", newHeading.trueHeading);
}

任何人都可以告诉我现在应该做什么让箭头指向该位置 - 即使iPhone被旋转了吗?

1 个答案:

答案 0 :(得分:8)

我有时间再玩这个。

这样做:

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];

    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D user = [location coordinate];

    [self calculateUserAngle:user];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocationCoordinate2D here =  newLocation.coordinate;

    [self calculateUserAngle:here];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    compass.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading * M_PI / 180);
    needle.transform = CGAffineTransformMakeRotation((degrees - newHeading.magneticHeading) * M_PI / 180);
}

-(void) calculateUserAngle:(CLLocationCoordinate2D)user {
    locLat = [[targetLocationDictionary objectForKey:@"latitude"] floatValue];
    locLon = [[targetLocationDictionary objectForKey:@"longitude"] floatValue];

    NSLog(@"%f ; %f", locLat, locLon);

    float pLat;
    float pLon;

    if(locLat > user.latitude && locLon > user.longitude) {
        // north east

        pLat = user.latitude;
        pLon = locLon;

        degrees = 0;
    }
    else if(locLat > user.latitude && locLon < user.longitude) {
        // south east

        pLat = locLat;
        pLon = user.longitude;

        degrees = 45;
    }
    else if(locLat < user.latitude && locLon < user.longitude) {
        // south west

        pLat = locLat;
        pLon = user.latitude;

        degrees = 180;
    }
    else if(locLat < user.latitude && locLon > user.longitude) {
        // north west

        pLat = locLat;
        pLon = user.longitude;

        degrees = 225;
    }

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

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

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