ios - getting user location and creating map not in sync -
the below code getting user current location , creating mapbox map. problem mapbox map being created before users location obtain. how may slow or sync process? thank 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 resources can recreated. } /*** mapbox functions ************************************************************************/ /*create preliminary map */ func createmapboxmap(){ // set access token let mapview = mglmapview(frame: view.bounds, accesstoken: "pk.eyj1ijoizgfya2zhzgvyiiwiysi6ilplvdhfr3mifq.ppez732qs8g0wescditakg") mapview.autoresizingmask = .flexiblewidth | .flexibleheight // set 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 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
location manager running asynchronously (expected behavior), means returns , finishes creating map create map box method called. however, doesn't mean map has gotten location yet. i'm thinking best way implement move self.createmapboxmap()
inside didupdatelocations
, try that. experience, want creation of view happen in callback asynchronous method this. might want have type of loading view though, because user confused @ first.
Comments
Post a Comment