我也对这个非常烦人的错误感到沮丧。
我的最后期限快到了,所以我不能花很多时间尝试实施解决方法。
我设法让它在最大缩放时保持在 MKUserTrackingModeFollowWithHeading 中,但是用户位置注释“pin”仍然非常严重地抖动,并且在某些边缘情况下,它仍然取消回到 MKUserTrackingModeFollow。
最初,我所做的是在 regionDidChangeAnimated: 委托方法中使用 BOOL 标志强制进行更正,如下所示:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
NSLog(@"regionWillChangeAnimated:");
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
NSLog(@"regionDidChangeAnimated:");
[self forceCorrectUserTrackingMode];
}
- (void)forceCorrectUserTrackingMode {
if (shouldFollowWithHeading == YES && ([mapView userTrackingMode] != MKUserTrackingModeFollowWithHeading) ) {
NSLog(@"FORCE shouldFollowWithHeading! - setUserTrackingMode:MKUserTrackingModeFollowWithHeading");
[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
} else if (shouldFollowWithHeading == NO && ([mapView userTrackingMode] != MKUserTrackingModeNone) ) {
NSLog(@"FORCE should NOT FollowWithHeading - setUserTrackingMode:MKUserTrackingModeNone");
[self.mapView setUserTrackingMode:MKUserTrackingModeNone animated:YES];
}
}
这实际上让我非常接近,但它不够可靠,就像我说的,我不得不考虑在我的最后期限内优先考虑其他功能,所以这就是我最终要做的:
首先,我在 MKMapKit 上获取了缩放类别的代码:http: //troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
接下来,我在该博客的评论中包含了访问者提供的这个方法:
- (int) getZoomLevel {
return 21 – round(log2(mapView.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapView.bounds.size.width)));
}
最后,在测试缩放级别与错误发生的情况下进行了一些试验和错误,导致我找到了以下“解决方法”:
CLLocationCoordinate2D userLocation_CenterCoordinate = CLLocationCoordinate2DMake([locationManager location].coordinate.latitude, [locationManager location].coordinate.longitude);
int currentZoomLevel = [MKMapView getZoomLevelForMapView:mapView];
int newZoomLevel = 17;
if (currentZoomLevel > 17) {
NSLog(@"Adjusting mapView's zoomLevel from [%d] to [%d], also centering on user's location", currentZoomLevel, newZoomLevel);
[mapView setCenterCoordinate:userLocation_CenterCoordinate zoomLevel:newZoomLevel animated:NO];
} else {
NSLog(@"Centering on user's location, not adjusting zoom.");
[mapView setCenterCoordinate:userLocation_CenterCoordinate animated:NO];
}