MKMapView doesn't zoom correctly while user tracking mode is MKUserTrackingModeFollowWithHeading

蓝咒 提交于 2019-11-28 08:34:06

From apple documentation:

Setting the tracking mode to MKUserTrackingModeFollow or MKUserTrackingModeFollowWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.

If you want both to adjust the region and to track the user, I suggest you check for location updates and adjust zoom accordingly.

For example:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 200.0f, 200.0f);
    [self.mapView setRegion:region animated:YES];
}

EDIT

Instead of setting the region, try just setting the center,

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

and let your button action set the zoom, keeping the same center:

- (IBAction)changeRegion:(id)sender {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.centerCoordinate, 200.0f, 200.0f);
    [self.mapView setRegion:region animated:YES];
}

And very important: do not set your mapView to track user. Disable tracking user because now you are tracking it yourself. I think the default is MKUserTrackingModeNone .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!