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

霸气de小男生 提交于 2019-11-27 02:18:15

问题


I created a test project with few lines of code and with two components: MKMapView and UIButton. I ticked mapView option - Shows user location. Also I defined an action for the button, it zooms the map to user location.

Here is code from controller:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    self.mapView.delegate = self;
}

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

Pretty simple and straightforward, isn't it? But when I tap the button I see weird behaviour: map view zooms to specified region then returns back to original zoom. What's the problem? How can I keep zooming and track user location at the same time?

I notice similar behaviour with MKUserTrackingModeFollow tracking mode.

P.S. I forgot to mention that it's a problem mostly for iOS7


回答1:


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 .



来源:https://stackoverflow.com/questions/19518224/mkmapview-doesnt-zoom-correctly-while-user-tracking-mode-is-mkusertrackingmodef

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