getting user location and creating map not in sync

LuAndre

The below code is getting the user current location and creating a mapbox map. problem is that the mapbox map is being created before the users location is obtain. How may I slow or sync this process? Thank you in advance,

  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

rb612

Location manager is running asynchronously (expected behavior), which means that it returns immediately and finishes creating the map as soon as the create map box method is called. However, it doesn't mean that the map has gotten a location yet. I'm thinking that the best way to implement this would be to move self.createMapBoxMap() to inside didUpdateLocations and try that. From my experience, you want the creation of your view to happen in a callback from an asynchronous method like this. You might want to have some type of loading view though, because the user will be confused at first.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related