iosobjective-ciphonemapkitmkcircle

How to set the zoom level of the MKMap view making MKCircle(varying radius) visible?


My map view contains one circle which covers many pinpoints with one center point.Whenever the user clicks a button the map should be zoomed in such a level the circle is visible.The map should not be zoomed more or less it should show exact circle.When user logs out then circle will have new radius in next login.How to calculate the appropriate zoom level. My code:

-(void)showCircle{

   //calculate new radius
    long radius=[self calculateRadius];
    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);
    MKCoordinateSpan span;

    //calculate zoom level
    double radius=[circle radius]);
    double rad = radius + radius / 2;

    double scale = rad / 500;
    zoomLevel=(16 - log(scale) / log(2));

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

    [myMapView setRegion:region animated:YES];
}

Thank you in advance!


Solution

  • You can try : mapRectThatFits(:) or mapRectThatFits(:edgePadding:)

    Something like this, maybe, to zoom the map according to your circle :

    myMapView.visibleMapRect = [myMapView mapRectThatFits:circle.boundingMapRect];
    

    Hope this helps you :)