基于 Adam Hs 的想法,这是我在Swift 4
单元测试中的实现:
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)
}
}
单元测试:
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)
}
测试设计:
- 对 lat 和 long 使用不同的数字来查找变量混合的错误
- 四舍五入检查约 5 个有效小数位