Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 578
Senseful Avatar asked Apr 21 '14 07:04

Senseful


1 Answers

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.

like image 80
Sjoerd Perfors Avatar answered Oct 24 '22 10:10

Sjoerd Perfors