Based on Adam H.s idea, this is my implementation in Swift 4
with unit tests:
extension MKCoordinateRegion {
/// middle of the south edge
var south: CLLocation {
return CLLocation(latitude: center.latitude - span.latitudeDelta / 2, longitude: center.longitude)
}
/// middle of the north edge
var north: CLLocation {
return CLLocation(latitude: center.latitude + span.latitudeDelta / 2, longitude: center.longitude)
}
/// middle of the east edge
var east: CLLocation {
return CLLocation(latitude: center.latitude, longitude: center.longitude + span.longitudeDelta / 2)
}
/// middle of the west edge
var west: CLLocation {
return CLLocation(latitude: center.latitude, longitude: center.longitude - span.longitudeDelta / 2)
}
/// distance between south and north in meters. Reverse function for MKCoordinateRegionMakeWithDistance
var latitudinalMeters: CLLocationDistance {
return south.distance(from: north)
}
/// distance between east and west in meters. Reverse function for MKCoordinateRegionMakeWithDistance
var longitudinalMeters: CLLocationDistance {
return east.distance(from: west)
}
}
Unit test:
func testMKCoordinateRegionMakeWithDistance() {
// arbitrary parameters
let center = CLLocationCoordinate2DMake(49, 9)
let latitudinalMeters: CLLocationDistance = 1000
let longitudinalMeters: CLLocationDistance = 2000
let region = MKCoordinateRegionMakeWithDistance(center, latitudinalMeters, longitudinalMeters)
XCTAssertEqual(latitudinalMeters, round(region.latitudinalMeters*100)/100)
XCTAssertEqual(longitudinalMeters, round(region.longitudinalMeters*100)/100)
}
test design:
- use different numbers for lat and long to find errors where variables are mixed up
- rounding checks for about 5 significant decimal places