Restrict MKMapView scrolling

后端 未结 6 1571
独厮守ぢ
独厮守ぢ 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:17

    SWIFT 5

    simple solution for use inside mapViewDidFinishLoadingMap:

        func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
    
            //center of USA, roughly. for example
            let center = CLLocationCoordinate2D(latitude: 38.573936, longitude: -92.603760) 
            let latMeters = CLLocationDistance(10_000_000.00) //left and right pan
            let longMeters = CLLocationDistance(5_000_000.00) //up and down pan
            
            let coordinateRegion = MKCoordinateRegion(
                center: center,
                latitudinalMeters: latMeters,
                longitudinalMeters: longMeters)
            
            let cameraBoundary = MKMapView.CameraBoundary(coordinateRegion: coordinateRegion)
            mapView.setCameraBoundary(cameraBoundary, animated: true)
        }
    

提交回复
热议问题