【发布时间】:2013-05-17 20:42:06
【问题描述】:
我有 MKMapView 和 MKUserTrackingModeFollowWithHeading。 但是有些东西将 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。
【问题讨论】:
-
我认为这个崩溃(MKUserTrackingModeFollowWithHeading crash)在 SO 上很流行,你应该看看其他关于这个崩溃的 SO 帖子
-
我正在寻找 SO 但我找不到...谢谢。
-
不是强制重置跟踪模式,你不能把
scrollEnabled设置为NO吗?这样用户可以缩放,但不能滚动,因此用户跟踪模式不应该因为用户交互而改变,不是吗?另外,如果这不是您当前所处的模式,您不应该只设置跟踪模式吗?如果didChangeUserTrackingMode设置成你想要的模式,就不用再设置用户跟踪模式了。 -
我尝试了
scrollEnabled = NO,但应用程序仍然崩溃。但是您在下面给出的答案似乎可以解决问题。谢谢。
标签: ios objective-c mapkit core-location