我有MKUserTrackingModeFollowWithHeading的MKMapView。 但是很多事情改變了userTrackingMode到MKUserTrackingModeFollow或無, 所以我實現了,放大時使用MKMapView在setUserTrackingMode上崩潰
- (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];
}
}
一切都很好,但每次我在地圖最詳細級別縮放,該應用會EXC_BAD_ACCESS在該行setUserTrackingMode:以上MKUserTrackingModeFollowWithHeading所示。
我該怎麼做才能避免崩潰?如果可能,我不想使用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崩潰)是一種對SO的流行,你應該看看比其他SO張貼關於這個崩潰 – danypata
我一直在尋找的SO,但我找不到......謝謝。 – kinamin
而不是強制重置跟蹤模式,你不能只將'scrollEnabled'設置爲'NO'?這樣用戶可以縮放,但不能滾動,因此用戶跟蹤模式不應該因用戶交互而改變,如果它?另外,如果這不是您目前使用的模式,您是不是應該只設置跟蹤模式?如果'didChangeUserTrackingMode'將其設置爲您想要的模式,則不需要再次設置用戶跟蹤模式。 – Rob