ios - 放大时使用 MKMapView 在 setUserTrackingMode 上崩溃

标签 ios objective-c mapkit core-location

我有带 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];
    }
}

一切都很好,但每次我将 map 放大到最详细级别时,应用程序都会在上面显示的 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 .

最佳答案

我可能会建议尝试推迟跟踪模式的设置,例如:

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(),^{
        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];
        }
    });
}

我可能还建议检查以确保 mode 不是您想要的,从而消除多余的 setUserTrackingMode:

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    MKUserTrackingMode newMode = MKUserTrackingModeNone;

    if ([CLLocationManager locationServicesEnabled]) {
        if ([CLLocationManager headingAvailable]) {
            newMode = MKUserTrackingModeFollowWithHeading;
        }else{
            newMode = MKUserTrackingModeFollow;
        }
    }

    if (mode != newMode)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.myMapView setUserTrackingMode:newMode animated:YES];
        });
    }
}

您还可以将它与 scrollEnabled 结合使用(这应该可以防止用户意外启动跟踪模式的更改)。

关于ios - 放大时使用 MKMapView 在 setUserTrackingMode 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16617563/

相关文章:

iphone - 使用 MapKit 检索 UserLocation 坐标

iphone - Monotouch EventKit - 无法检索 EventIdentifier

ios - 这个 "Redefinition of ' 组''错误是什么?

ios - 在 iOS 中解析字符串

ios - 在 Objective-C 中的方法之间传递信息

ios - 增强现实测量 - iPhone

ios - 在 iOS 7 上成为第一响应者后,UITextView 不会滚动到插入符号

objective-c - 直接访问子类中自动合成的实例变量?

swift - Swift 中的 MapKit,第 3 部分 - MKPointAnnotation,addAnnotations

ios - 默认显示当前位置标注