kin*_*min 3 objective-c core-location mapkit ios
我有带有 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];
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,但每次我将地图放大到最详细级别时,应用程序都会在上面显示的 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];
}
Run Code Online (Sandbox Code Playgroud)
任何形式的建议表示赞赏。先感谢您。
我将最少的示例代码上传到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];
}
});
}
Run Code Online (Sandbox Code Playgroud)
我可能还建议检查以确保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];
});
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以将其与scrollEnabled(这应该可以防止用户意外启动跟踪模式的更改)结合起来。
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |