-->

can't set zoom level on MKMapView

2019-07-31 09:48发布

问题:

i am adding MKCircleView to the user annotation like so :

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!_MapCentered) {
    **_circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:3000];
    [_map_view addOverlay:_circle];** 
    _MapCentered = YES;
    }
}

it will fire once and once the user location has traced, it works well but as you can see the diameter of the circle view is 3000 meters. so now i want the zoom level to fit the CircleView like so :

        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 0.270, 0.270);
    [_map_view setRegion:viewRegion animated:YES];

i have changed the delta degrees to other numbers but nothing is changed. how can i manage this?

回答1:

You need to set your span. so set your span value in longitudeDelta & latitudeDelta

yourRegion.span.longitudeDelta  = 0.004; // set required zoom value 
yourRegion.span.latitudeDelta  = 0.004; // set required zoom value


回答2:

The distance parameters in the MKCoordinateRegionMakeWithDistance function are in meters (not degrees).

Also, the meters specify the full width and height so you have to use double the value of the circle's radius.

So it should be:

MKCoordinateRegion viewRegion = 
  MKCoordinateRegionMakeWithDistance
    (mapView.userLocation.coordinate, 6000, 6000);


You could also just set the map view's visibleMapRect to the boundingMapRect of the circle overlay so you don't have to repeat the distance values:

mapView.visibleMapRect = _circle.boundingMapRect;


回答3:

For Google's zoom level i use this category for MKMapView

Otherwise use Anna's solution