Search code examples
iphoneobjective-cmkmapviewzooming

how to make mapview zoom to 5 mile radius of current location


I know its a very common issue but I am not getting the exact answer for this thing.

How to make MKMapView defaults to a zoom of a 5 mile radius of current location.

Thanks in advance.


Solution

  • Use the following code when ever you want to zoom to 5 miles radius:

    double miles = 5.0;
    double scalingFactor = ABS( (cos(2 * M_PI * newLocation.coordinate.latitude / 360.0) ));
    
    MKCoordinateSpan span; 
    
    span.latitudeDelta = miles/69.0;
    span.longitudeDelta = miles/(scalingFactor * 69.0); 
    
    MKCoordinateRegion region;
    region.span = span;
    region.center = newLocation.coordinate;
    
    [mapView setRegion:region animated:YES];