【问题标题】:Point to location using compass使用指南针指向位置
【发布时间】:2011-07-02 20:18:37
【问题描述】:

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

我已经计算了从用户位置到注释位置的距离,并且我得到了 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 是旋转的?

【问题讨论】:

    标签: iphone sdk location compass-geolocation heading


    【解决方案1】:

    我有时间再玩一次。

    这样就可以了:

    - (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);
    }
    

    【讨论】:

    • 这太棒了..我会在我的项目中尝试它,我希望它能正常工作..你拯救了我的一天:)
    • 你能把这个完整的课程发给我吗:)
    • 是的,这些信息真的很有帮助。你能回答我与此相关的问题吗? - stackoverflow.com/questions/7173925/…
    • @lesk 不使用代码来计算用户角度,而是使用问题代码。在问题结果 = 度和对我来说工作正常
    • 我认为还值得记住的是,包含箭头的 UIImage 必须是垂直箭头,即当您将箭头添加到 xib 时,箭头图像直接指向顶部屏幕
    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多