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

℡╲_俬逩灬. 提交于 2019-12-04 16:23:58

问题


I am using a MKUserTrackingBarButtonItem button to allow the user to automatically track their location on a map. The problem is that when they tap this button, it is zoomed too far out. I want it to start at a specified zoom level (i.e. span). How can I achieve this?

When the user taps the button to change to MKUserTrackingModeFollow, it seems to use the same zoom level that the user last manually changed to (i.e. using gestures on the map). Attempting to specify a different zoom level via setRegion or setVisibleMapRect does not affect what zoom level will be used when the mode is changed to MKUserTrackingModeFollow.

Attempting to override mapView:didChangeUserTrackingMode: to set the region causes the mode to be changed back to MKUserTrackingModeNone. Example:

- (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];
    }
}

If I attempt to reset the mode immediately after setting the region, it works fine if the user is stationary, but zooms back out if the user is moving.

The simplest solution would be if there was a way to simply specify something like a zoom level for MKUserTraking by sending it my span value. However, since that doesn't seem to exist, what else can I do?


回答1:


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.




回答2:


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];


来源:https://stackoverflow.com/questions/23192956/how-do-i-specify-the-zoom-level-when-using-an-mkusertrackingbarbuttonitem

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