MKMapView zoom to users location on viewDidLoad?

前端 未结 10 1705
有刺的猬
有刺的猬 2021-01-30 16:40

I\'m trying to zoom a map into the user\'s current location once the view loads, but I\'m getting the error \"** * Terminating app due to uncaught exception \'NSInvalidA

10条回答
  •  广开言路
    2021-01-30 17:26

    Here is @Deepak's answer in Swift 3 for iOS 10:

    extension ViewController: MKMapViewDelegate
    {
        func mapView( _ mapView: MKMapView, didUpdate userLocation: MKUserLocation )
        {
            let regionRadius = 400 // in meters
            let coordinateRegion = MKCoordinateRegionMakeWithDistance( userLocation.coordinate, regionRadius, regionRadius )
            self.mapView.setRegion( coordinateRegion, animated: true)
        }
    }
    

提交回复
热议问题