获取用户位置和创建地图不同步

getting user location and creating map not in sync

以下代码获取用户当前位置并创建 mapbox 地图。 问题是在获取用户位置之前正在创建 mapbox 地图。 我怎样才能减慢或同步这个过程? 提前谢谢你,

  import UIKit
  import CoreLocation
  import MapboxGL

 class AViewController: UIViewController, CLLocationManagerDelegate {
   var manager:CLLocationManager!
   var userLocation:CLLocation = CLLocation(latitude: 25.776243, longitude: -80.136509)

override func viewDidLoad() {
    super.viewDidLoad()

    println("inside viewdidload")
    self.getUserLocation()
}//eom

override func viewDidAppear(animated: Bool) {
    println("inside viewdidappear")
   self.createMapBoxMap()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*** MapBox Functions ************************************************************************/
    /*Create preliminary map */
    func createMapBoxMap(){
        // set your access token
        let mapView = MGLMapView(frame: view.bounds, accessToken: "pk.eyJ1IjoiZGFya2ZhZGVyIiwiYSI6IlplVDhfR3MifQ.pPEz732qS8g0WEScdItakg")

        mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight

        // set the map's center coordinate
        mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: self.userLocation.coordinate.latitude, longitude: self.userLocation.coordinate.longitude),
            zoomLevel: 13, animated: false)
        view.addSubview(mapView)

        //showing the user location on map - blue dot
        mapView.showsUserLocation = true
    }//eom


/*** location Functions ************************************************************************/
    /*getting user current location*/
    func getUserLocation(){
        self.manager = CLLocationManager()
        self.manager.delegate = self
        self.manager.desiredAccuracy = kCLLocationAccuracyBest
        self.manager.requestWhenInUseAuthorization()
        self.manager.startUpdatingLocation()
    }//eom

    /*location manager 'didUpdateLocations' function */
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        self.manager.stopUpdatingLocation() //stop getting user location
        println(locations)

        self.userLocation = locations[0] as! CLLocation

    }//eom

    /* errors occurred */
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
        println("Error:" + error.localizedDescription)
    }//eom

}//eoc

位置管理器是 运行 异步的(预期行为),这意味着它会立即 returns 并在调用创建地图框方法后立即完成地图创建。但是,这并不意味着地图已经找到了位置。我认为实现此目的的最佳方法是将 self.createMapBoxMap() 移动到 didUpdateLocations 内部并尝试这样做。根据我的经验,您希望视图的创建发生在像这样的异步方法的回调中。不过,您可能想要某种类型的加载视图,因为用户一开始会感到困惑。