crash on setUserTrackingMode with MKMapView when zoomed in

瘦欲@ 提交于 2019-12-01 08:24:02

I might suggest trying to defer the setting of the tracking mode, e.g.:

- (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];
        }
    });
}

I might also suggest checking to make sure the mode isn't already what you want, eliminating a redundant 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];
        });
    }
}

You could also combine that with scrollEnabled (which should prevent the user from incidentally initiating a changing of the tracking mode).

Swift 3

func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode, animated: Bool) {        
        var newMode: MKUserTrackingMode = .none
        if ( CLLocationManager.locationServicesEnabled() ) {
            if ( CLLocationManager.headingAvailable() ) {
                newMode = .followWithHeading
            }
            else {
                newMode = .follow
            }
        }

        if (mode != newMode)
        {
            DispatchQueue.main.async {
                mapView.setUserTrackingMode(newMode, animated: true)
            }
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!