制作指南针iPhone 3GS

时间:2013-06-03 06:11:30

标签: ios core-location cllocationmanager

我正在制作罗盘应用程序,但当我的函数[localManager startUpdatingHeading]调用它时会自动调用函数

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

但是第二个函数从未调用过,所以我的程序不起作用。我在我的设备上运行此代码,没有任何反应。请帮帮我。

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationManager *locaManager = [[CLLocationManager alloc] init];
    locaManager.desiredAccuracy = kCLLocationAccuracyBest;
    locaManager.delegate = self;
    locaManager.headingFilter = .5;
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager
                                                    headingAvailable]) {
        [locaManager startUpdatingHeading];
        [locaManager startUpdatingLocation];
    } else {
    NSLog(@"Error");
    }
}


- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading     *)newHeading {
    if (newHeading.headingAccuracy > 0) {
        float magneticHeading = newHeading.magneticHeading;
        float trueHeading = newHeading.trueHeading;
        label2.text = [NSString stringWithFormat:@"%f", magneticHeading];
        label1.text = [NSString stringWithFormat:@"%f", trueHeading];
        float heading = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
        imagen.transform = CGAffineTransformMakeRotation(heading);
    }

}

2 个答案:

答案 0 :(得分:0)

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.headingFilter = kCLHeadingFilterNone;
    [locationManager startUpdatingHeading];

    [self.view bringSubviewToFront:_compass_image];



- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//    [manager stopUpdatingHeading];

    double rotation = newHeading.magneticHeading * 3.14159 / 180;
//    CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin

    //[mapView.map setTransform:CGAffineTransformMakeRotation(-rotation)];
    [_compass_image setTransform:CGAffineTransformMakeRotation(-rotation)];

//    [[mapView.map annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        MKAnnotationView * view = [mapView.map viewForAnnotation:obj];
//        
//        [view setTransform:CGAffineTransformMakeRotation(rotation)];
//        [view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];
//        
//    }];

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if ([error code] == kCLErrorDenied)
    {
        // This error indicates that the user has denied the application's request to use location services.
        [manager stopUpdatingHeading];
    }
    else if ([error code] == kCLErrorHeadingFailure)
    {
        // This error indicates that the heading could not be determined, most likely because of strong magnetic interference.
    }
}

答案 1 :(得分:0)

.h 文件中声明此变量  CLLocationManager * locaManager;

.m 文件

中执行此操作
- (void)viewDidLoad
{
    [super viewDidLoad];
    locaManager = [[CLLocationManager alloc] init];
    locaManager.desiredAccuracy = kCLLocationAccuracyBest;
    locaManager.delegate = self;
    locaManager.headingFilter = .5;                                         
    [locaManager startUpdatingHeading];
    [locaManager startUpdatingLocation];

}