Check whether zoom level changed

后端 未结 7 1922
余生分开走
余生分开走 2020-12-23 12:05

I\'m using MapKit on iPhone. How can I know when the user changes the zoom level (zoom in\\out the map)?

I\'ve tried to use mapView:(MKMapView *)mapView regi

7条回答
  •  余生分开走
    2020-12-23 12:59

    Much more simpler answer:

    The easiest way to get an Integer of the current zoom level, is by using the MapView function: regionDidChangeAnimated. This function recognizes every change in zoom and will give you the basis for the calculation of the zoom factor.

    Just insert this function into your MapView class (works for Swift 3.0):

    var mapView: MKMapView! = nil
    
    ...
    
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let zoomWidth = mapView.visibleMapRect.size.width
        let zoomFactor = Int(log2(zoomWidth)) - 9
        print("...REGION DID CHANGE: ZOOM FACTOR \(zoomFactor)")
    }
    

    And you will get a zoomFactor value out of it, where 0 is the most near point you can zoom into the map and every higher value is a far far away zoom... :-)

提交回复
热议问题