MKCoordinateRegionMakeWithDistance
can be used to make a region with a specified distance (in meters).
Here is the method signature:
func MKCoordinateRegionMakeWithDistance(_ centerCoordinate: CLLocationCoordinate2D,
_ latitudinalMeters: CLLocationDistance,
_ longitudinalMeters: CLLocationDistance) -> MKCoordinateRegion
Use an extension to get your desired distance and convert it into meters.
extension Double {
func convert(from originalUnit: UnitLength, to convertedUnit: UnitLength) -> Double {
return Measurement(value: self, unit: originalUnit).converted(to: convertedUnit).value
}
}
Then, adjust the region using MKMapView's regionThatFits()
method and plug in the desired region. MKMapView
will figure the adjusted region and lastly, set the new adjusted region.
let viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2D(latitude: latitude, longitude: longitude), miles.convert(from: .miles, to: .meters), miles.convert(from: .miles, to: .meters))
let adjustedRegion = mapView.regionThatFits(viewRegion)
mapView.setRegion(adjustedRegion, animated: true)