3

MKUserTrackingModeFollowWithHeading を使用した MKMapView があります。しかし、何かが userTrackingMode を MKUserTrackingModeFollow または None に変更したので、実装しました。

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    if ([CLLocationManager locationServicesEnabled]) {
        if ([CLLocationManager headingAvailable]) {
            [self.myMapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:NO];
        }else{
            [self.myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
        }
    }else{
        [self.myMapView setUserTrackingMode:MKUserTrackingModeNone animated:NO];
    }
}

すべて問題ありませんが、マップを最も詳細なレベルにズームインするたびに、アプリは上記の setUserTrackingMode:MKUserTrackingModeFollowWithHeading 行で EXC_BAD_ACCESS を引き起こします。

クラッシュしないようにするにはどうすればよいですか? 可能であれば MKUserTrackingBarButtonItem を使用したくありません。

mapViewController の他の部分を以下に示します。

- (void)dealloc
{
    self.myMapView.delegate = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    if ([CLLocationManager locationServicesEnabled]) {
        self.myMapView.showsUserLocation = NO;
        [_locManager stopUpdatingLocation];

        if ([CLLocationManager headingAvailable]) {
            [_locManager stopUpdatingHeading];
        }
    }

    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    if ([CLLocationManager locationServicesEnabled]) {
        self.myMapView.showsUserLocation = YES;
        [_locManager startUpdatingLocation];

        if ([CLLocationManager headingAvailable]) {
            [self.myMapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:NO];
            [_locManager startUpdatingHeading];
        }else{
            [self.myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
        }
    }else{
        [self.myMapView setUserTrackingMode:MKUserTrackingModeNone animated:NO];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.myMapView.delegate = self;
    [self.myMapView setFrame:self.view.frame];

    self.locManager = [CLLocationManager new];
    [self.locManager setDelegate:self];
    [self.locManager setDistanceFilter:kCLDistanceFilterNone];
    [self.locManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locManager setHeadingFilter:3];
    [self.locManager setHeadingOrientation:CLDeviceOrientationPortrait];
}

どんな種類のアドバイスも大歓迎です。前もって感謝します。

最小限のサンプルコードをgithubにアップロードします。

4

2 に答える 2