Display MKMapViewAnnotations within a map view's visible rect

后端 未结 7 751
梦毁少年i
梦毁少年i 2021-02-02 02:31

I\'m displaying an MKMapView inside a Path-style parallax table view header. To create the effect, the mapView bounds is larger than the area visible to the user. I need to set

7条回答
  •  不要未来只要你来
    2021-02-02 03:00

    I found an easier way without calculating is let the map view calculate it, then we adjust the edges.

    //1: Show all annotation on the map view, but without animation
    self.mapView.showAnnotations(self.mapView.annotations, animated: false)
    
    //2: Get the current visible map rect
    let currentMapRect = self.mapView.visibleMapRect
    
    //3: Create the edges inset that you want the map view to show all annotation within
    let padding = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)
    
    //4: Set current map rect but with new padding, also set animation to true to see effect
    self.mapView.setVisibleMapRect(currentMapRect, edgePadding: padding, animated: true)
    

提交回复
热议问题