26

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.

Aisha
  • 1,559
  • 5
  • 20
  • 37

3 Answers3

60

Use the MKCoordinateRegionMakeWithDistance function from MapKit.

Use a conversion function to convert miles to meters since MapKit uses meters.

float MilesToMeters(float miles) {
    // 1 mile is 1609.344 meters
    // source: http://www.google.com/search?q=1+mile+in+meters
    return 1609.344f * miles;
}

Then in your code set the map region as (thanks to @DylanBettermann for pointing out that to get a radius of 5 miles, the distance needs to be doubled)

mapView.region = MKCoordinateRegionMakeWithDistance(
    centerCoordinate, 
    MilesToMeters(10.0f),
    MilesToMeters(10.0f)
);

swift 4 version :

mapView.region = MKCoordinateRegion(
            center: centerCoordinate,
            latitudinalMeters: MilesToMeters(10.0f),
            longitudinalMeters: MilesToMeters(10.0f)
)
Climbatize
  • 1,103
  • 19
  • 34
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 2
    I'm not sure if `MKCoordinateRegionMakeWithDistance` has changed since posting, but in iOS 9 this will only get you a 2.5 mile radius. Pass the diameter to `MKCooridinateRegionMakeWithDistance` instead of the radius. (e.g. `MilesToMeters(10.0f)` for a 5 mile radius) – Dylan Bettermann Jun 18 '15 at 04:12
  • @DylanBettermann - thanks for pointing that out. I don't think the API has changed as far as I know. It has always been the north-south, or east-west distance. Updated the sample code. – Anurag Sep 21 '15 at 07:09
55

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];
Satyam
  • 15,493
  • 31
  • 131
  • 244
  • In my code, fill up the "newLocation" variable with current location of user and it will zoom to 5 miles redius from current location. – Satyam Feb 17 '11 at 10:47
  • 31
    To anyone wondering, the magic number 69 is the number of miles per 60 nautical miles. There are 60 nautical miles in a degree of latitude. – btt Aug 15 '11 at 19:40
  • @StephenBurns It's depend on frame of the MKMapView? – Pramod Tapaniya Feb 13 '16 at 09:58
  • This answer does work but it gives the wrong distance. The correct way is to use Anurag's answer below. I upvoted this answer then started comparing both distance's on my mapView and this one was incorrect. Don't use this answer use the one below. – Lance Samaria Nov 03 '18 at 21:32
2
[self.theMapView setRegion:MKCoordinateRegionMake(
                    [theConsumer.latLong.clLocation coordinate], 
                    MKCoordinateSpanMake( MAP_SPAN, MAP_SPAN ))
                 animated:YES];

The parameters to MKCoordinateSpanMake are in degrees, and 1 degree is approx 69 miles at the equator. Thus MAP_SPAN would be 5.0/69.0

Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
Rayfleck
  • 12,116
  • 8
  • 48
  • 74