首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定MKCoordinateRegion是否在另一个MKCoordinateRegion中

如何确定MKCoordinateRegion是否在另一个MKCoordinateRegion中
EN

Stack Overflow用户
提问于 2020-10-22 12:24:29
回答 1查看 342关注 0票数 1

我试图将保存的区域与另一个区域进行比较,不管它是否在该区域内。每当用户放大地图时,该区域将被更改;当以下函数被调用时:

func mapView(_ mapView: MKMapView,regionDidChangeAnimated动画: Bool) {

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-22 13:14:06

您可以轻松地定义自己的函数,以检查MKCoordinateRegion是否包含另一个函数。一种方法是计算这个区域的最小/最大纬度和经度,然后比较你想要比较的两个区域之间的所有四个极值。

代码语言:javascript
复制
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) // false
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64482149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档