0

コンパスアプリケーションを作成していますが、関数[localManager startUpdatingHeading]が呼び出されると、関数が自動的に呼び出されます

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

しかし、2 番目の関数は呼び出されないため、プログラムは機能しません。このコードをデバイスで実行しましたが、何も起こりませんでした。私を助けてください。

- (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 に答える