MapKit zoom to user current location

后端 未结 8 514
慢半拍i
慢半拍i 2021-02-01 17:59

I am trying to simply show user\'s location on the map, but I need to when app launches, the map should zoom to current location ,but I don\'t know why map doesn\'t zoom at all

8条回答
  •  一向
    一向 (楼主)
    2021-02-01 18:33

    Here's another approach for Swift 3, XCode 8.2. First, write out a helper function:

    let homeLocation = CLLocation(latitude: 37.6213, longitude: -122.3790)
    let regionRadius: CLLocationDistance = 200
    func centerMapOnLocation(location: CLLocation)
    {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                               regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }
    

    Then, call in in viewDidLoad()

    mapView.showsUserLocation = true
    centerMapOnLocation(location: homeLocation)
    

    This will start the app with the location specified in the variable zoomed in.

提交回复
热议问题