How to calculate the zoom level of MKMapView to fit MKCircle?

551 views Asked by At

I want to show mkcircle at button click event.I have to calculated the zoom level of MKMapView using circle radius to make circle noticeable.Let the radius be anything small or big but mkcircle on mkmapview should be big enough as given in the screenshot.My code:

- (IBAction)showCircle:(id)sender {
    long radius=[self calculateRadius];
    NSLog(@"draw circle of radius=%ld",radius);
    MKCircle *circle= [[MKCircle alloc]init];
    circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake([groupLat floatValue], [groupLon floatValue]) radius:radius];
    [myMapView addOverlay:circle];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake([groupLat floatValue], [groupLon floatValue]), 800, 800);

    //calculate zoom level
    double radius = [circle radius] + [circle radius] / 2;
    double scale = radius / 500;
    zoomLevel=(16 - log(scale) / log(2));

    region.span.latitudeDelta  =zoomLevel;
    region.span.longitudeDelta =zoomLevel;

    [myMapView setRegion:region animated:YES];

//problem is circle looks very small and outside space is more

enter image description here
I want output like below

enter image description here

Any suggestions would be appreciated. thank you!

1

There are 1 answers

0
jpulikkottil On

I know its old post, But replying any way.. Code in Swift 5, Xcode 11

let region = MKCoordinateRegion(circle.boundingMapRect)
mapView.setRegion(region, animated: true)

Or we can add some margin easily by the following way

mapView.setVisibleMapRect(circle.boundingMapRect, edgePadding: UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50), animated: true)