How do I specify the zoom level when using an MKUserTrackingBarButtonItem?

前提是你 提交于 2019-12-03 10:22:47

I had the same issue and used a different approach to fix it. You can use the MapCamera function for this instead of that button.

On each new location do this:

 MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:[newLocation coordinate]
 fromEyeCoordinate:[oldLocation coordinate]
 eyeAltitude:2000];

 [mapView setCamera:newCamera animated:TRUE];

And play with the eyeAltitude.

If the user manually zooms in or out you can read the altitude value from mapview.camera.altitude also don't update the camera when the user is manually using the map.

According to apple documentation used here

https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode

Setting the tracking mode to follow or follow​With​Heading 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.

Here changing the region does not effect your visible region due to that reason.

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
    CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
    [mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
    // [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO];
 }
}

So you just need to change center coordinate on didChangeUserTrackingMode instead of changing the whole region

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated {
if (mode == MKUserTrackingModeFollow) {
   [self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
   }
 }

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

on click of MKUserTrackingBarButtonItem change the zoom level

 CLLocationCoordinate2D center = mapView.userLocation.location.coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717);
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!