代码之家  ›  专栏  ›  技术社区  ›  Jacob King

iOS MapKit-在地图完全放大时检测?

  •  1
  • Jacob King  · 技术社区  · 8 年前

    因此,我使用集群库对注释进行分组,其中有一个小错误,当地图完全放大时,一些非常接近的注释可能会出现分组。由于这是一个框架,我不能直接做很多事情,但如果地图完全放大,我可以禁用所有分组。问题是我找不出一个可靠的方法来做这件事。

    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 属性,但我相信这将根据屏幕大小等而改变。。。

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

    1 回复  |  直到 8 年前
        1
  •  1
  •   derdida    8 年前

    您需要扩展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