Restrict MKMapView scrolling

后端 未结 6 1557
独厮守ぢ
独厮守ぢ 2020-11-29 03:28

I\'m trying to add a custom image to an MKMapView as an MKOverlayView - I need to restrict users from being able to scroll outside the bounds of th

6条回答
  •  暖寄归人
    2020-11-29 04:19

    MapKit now does this natively in iOS 13

    You can explicitly set a boundary to restrict panning.

    let boundaryRegion = MKCoordinateRegion(...) // the region you want to restrict
    let cameraBoundary = CameraBoundary(region: boundaryRegion)
    mapView.setCameraBoundary(cameraBoundary: cameraBoundary, animated: true)
    

    See WWDC 2019 video at 2378 seconds for a demonstration.

    You can also restrict zoom levels

    let zoomRange = CameraZoomRange(minCenterCoordinateDistance: 100,
        maxCenterCoordinateDistance: 500)
    mapView.setCameraZoomRange(zoomRange, animated: true)
    

    References

    • MapKit documentation on "Constraining the Map View"
    • class reference for MKMapView.CameraBoundary
    • class reference for MKMapView.CameraZoomRange

提交回复
热议问题