微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

查找 MKMapView 的最大缩放级别,并知道何时达到

如何解决查找 MKMapView 的最大缩放级别,并知道何时达到

我看过很多关于设置最大缩放级别的内容,但没有看到如何获得 它。

这是交易。我正在为集群注释做一个“钻取”用户界面。虽然地图可以缩放,但点按集群只会使地图以集群为中心,并将跨度减半。

当它到达末尾时,仍然有一个簇,但是,我希望能够显示一个弹出框/标注,表示剩余簇的内容

我无法检查缩放,以了解我计划设置的新区域是否会“占用”。

我知道,一旦达到最大缩放,将不再调用 mapView(_ mapView: MKMapView,regionWillChangeAnimated animated: Bool),但这意味着我需要处理上下文,并进行相当复杂的舞蹈。

是否有一种简单的方法可以检查 MKMapView,并查看它是否处于最大缩放?

解决方法

好的,那就没有人了。

这是我如何做到的。笨重,但有效。

首先,我将私有实例属性设置为金丝雀:

private var _lastClusterTapped: Bool = false

接下来,我设置了一个简单的委托回调来清除金丝雀:

func mapView(_: MKMapView,regionWillChangeAnimated: Bool) {
    _lastClusterTapped = false
}

然后,在处理集群点击的代码中,我这样做了:

func tappedOnClusterAnnotation(mapView inMapView: MKMapView,annotationView inAnnotationView: MKAnnotationView) {        
    // We attempt to zoom in by a certain amount. We do this by dividing the span.
    if let coords = inAnnotationView.annotation?.coordinate,var region = inMapView?.region {
        // The new region will be half the size of the original map region.
        region.span.latitudeDelta /= 2.0
        region.span.longitudeDelta /= 2.0
        region.center = coords // The new region will center on the annotation.
        _lastClusterTapped = true
        inMapView?.setRegion(region,animated: true)
        // If the callback did not happen (canary is still alive),then we assume we are maxed out,and call the handler.
        if _lastClusterTapped {
            // Do whatever we do,when a "locked" annotation is tapped.
        }
    }
}

我讨厌金丝雀和信号量,但它确实有效。

如果区域回调以异步方式发生,则这具有不工作的显着缺点。它基于内联回调,因此在区域设置完成后金丝雀已被清除。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。