4

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!

sarita
  • 236
  • 1
  • 11
  • Hope one of these helps : http://stackoverflow.com/questions/7594827/how-to-find-current-zoom-level-of-mkmapview http://stackoverflow.com/questions/2960294/mkmapview-getting-the-relative-zoom-ratio – Mrunal Dec 29 '16 at 08:46
  • @Mrunal thanks for comment.But I have to adjust the zoom level according to the radius of circle which may vary.The above links didn't work for me.Any other suggestion please! – sarita Dec 31 '16 at 09:04

1 Answers1

0

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)
jpulikkottil
  • 666
  • 9
  • 24