1

所以我正在使用一个聚类库来对注释进行分组,并且有一个小错误,当地图完全放大时,一些非常接近的注释可能会出现分组。作为一个框架,我不能直接做很多事情但是如果地图完全放大,我可以禁用所有分组。问题是我无法找到可靠的方法来做到这一点。

这是我的regionDidChangeAnimated代码,理想情况下,我想检查地图是否完全放大(到无法再放大的程度)。

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    NSOperationQueue().addOperationWithBlock { 
        let scale: Double = Double(self.map.bounds.size.width) / self.map.visibleMapRect.size.width
        let annotations = self.clusteringManager?.clusteredAnnotationsWithinMapRect(self.map.visibleMapRect, withZoomScale: scale)
        self.clusteringManager?.displayAnnotations(annotations, onMapView: self.map)
    }
}

我已经尝试检查该mapView.region.span属性,但我确信这会根据屏幕大小等而改变......

有什么建议么?提前致谢。

4

1 回答 1

1

您需要扩展您的 MKMapView:

class YourMapView : MKMapView {

    // function returns current zoom level of the map

    func getCurrentZoom() -> Double {

        var angleCamera = self.camera.heading
        if angleCamera > 270 {
            angleCamera = 360 - angleCamera
        } else if angleCamera > 90 {
            angleCamera = fabs(angleCamera - 180)
        }

        let angleRad = M_PI * angleCamera / 180 

        let width = Double(self.frame.size.width)
        let height = Double(self.frame.size.height)

        let offset : Double = 20 // offset of Windows (StatusBar)
        let spanStraight = width * self.region.span.longitudeDelta / (width * cos(angleRad) + (height - offset) * sin(angleRad))
        return log2(360 * ((width / 256) / spanStraight)) + 1;
      }

}

现在您可以在以下委托方法中读出当前的缩放级别:

regionDidChangeAnimated

regionWillChangeAnimated
于 2016-09-28T11:13:49.050 回答