我试图将保存的区域与另一个区域进行比较,不管它是否在该区域内。每当用户放大地图时,该区域将被更改;当以下函数被调用时:
func mapView(_ mapView: MKMapView,regionDidChangeAnimated动画: Bool) {
发布于 2020-10-22 13:14:06
您可以轻松地定义自己的函数,以检查MKCoordinateRegion是否包含另一个函数。一种方法是计算这个区域的最小/最大纬度和经度,然后比较你想要比较的两个区域之间的所有四个极值。
extension MKCoordinateRegion {
var maxLongitude: CLLocationDegrees {
center.longitude + span.longitudeDelta / 2
}
var minLongitude: CLLocationDegrees {
center.longitude - span.longitudeDelta / 2
}
var maxLatitude: CLLocationDegrees {
center.latitude + span.latitudeDelta / 2
}
var minLatitude: CLLocationDegrees {
center.latitude - span.latitudeDelta / 2
}
func contains(_ other: MKCoordinateRegion) -> Bool {
maxLongitude >= other.maxLongitude && minLongitude <= other.minLongitude && maxLatitude >= other.maxLatitude && minLatitude <= other.minLatitude
}
}
let largerRegion = MKCoordinateRegion(MKMapRect(x: 50, y: 50, width: 100, height: 100))
let smallerRegion = MKCoordinateRegion(MKMapRect(x: 70, y: 50, width: 30, height: 80))
let otherRegion = MKCoordinateRegion(MKMapRect(x: -100, y: 0, width: 100, height: 80))
largerRegion.contains(smallerRegion) // true
largerRegion.contains(otherRegion) // false
smallerRegion.contains(otherRegion) // falsehttps://stackoverflow.com/questions/64482149
复制相似问题