一种方法是定义辅助方法,以便使用 MapKit 和 CoreLoation 根据给定距离(以米为单位)的坐标轻松计算新坐标,如下所示:
// Create new coordinate with equal latitude and longitude distances (distance can be both positive and negative).
func coordinate(from coordinate: CLLocationCoordinate2D, distance: CLLocationDistance) -> CLLocationCoordinate2D {
return self.coordinate(from: coordinate, latitudeDistance: distance, longitudeDistance: distance)
}
// Create new coordinate with latitude and longitude distances (distances can be both positive and negative).
func coordinate(from coordinate: CLLocationCoordinate2D, latitudeDistance: CLLocationDistance, longitudeDistance: CLLocationDistance) -> CLLocationCoordinate2D {
let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudeDistance, longitudeDistance)
let newLatitude = coordinate.latitude + region.span.latitudeDelta
let newLongitude = coordinate.longitude + region.span.longitudeDelta
return CLLocationCoordinate2D(latitude: newLatitude, longitude: newLongitude)
}
那么上面的代码看起来就像这样:
let coordinate = CLLocationCoordinate2D(latitude: 9.0, longitude: 50.0)
let distance: CLLocationDistance = 5000 // 5km
let topLeftCoordinate = self.coordinate(from: coordinate, distance: -distance)
let bottomRightCoordinate = self.coordinate(from: coordinate, distance: distance)
希望它在遇到此问题时对那里的人有所帮助。
预防信息:以防万一有人认为(正如许多人认为的那样)自我回答的问题不适合 SO,请先阅读此内容。