0

我正在制作一个指南针应用程序,但是当我的函数[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);
    }

}
4

2 回答 2

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.
    }
}
于 2013-06-03T06:29:47.030 回答
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];

}
于 2013-06-03T06:41:21.527 回答