How should I find the correct location data using userLocation in Mapbox for iOS SDK? (Swift) -
i'm trying find latitude , longitude of user's location can center map on user in viewdidload.
i've implemented seems right code values of userlat (latitude) , userlon (longitude) way off.
n.b. else had same problem me answer never resolved: mapbox ios8 swift mapview.showuserslocation
import mapbox class viewcontroller: uiviewcontroller, mglmapviewdelegate, cllocationmanagerdelegate { // let locationmanager = cllocationmanager() @iboutlet weak var mapview: mglmapview! override func viewdidload() { super.viewdidload() // initalise map's center coordinate vancouver mapview.setcentercoordinate(cllocationcoordinate2d(latitude: 49.283382, longitude: -123.117394), zoomlevel: 15, animated: false) view.addsubview(mapview) // set delegate property of our map view self after instantiating it. mapview.delegate = self // user location mapview.showsuserlocation = true let userloc = mapview.userlocation! userloc.title = "hello" userloc.subtitle = "i here!" let userlat = userloc.coordinate.latitude let userlon = userloc.coordinate.longitude print(userlat, userlon) /* self.locationmanager.requestalwaysauthorization() if cllocationmanager.locationservicesenabled() { locationmanager.delegate = self locationmanager.desiredaccuracy = kcllocationaccuracynearesttenmeters locationmanager.startupdatinglocation() }*/ } func mapview(mapview: mglmapview, annotationcanshowcallout annotation: mglannotation) -> bool { return true } }
resulting print:
3.40282346638529e+38 3.40282346638529e+38
the strange thing annotation works fine, , when click location title , subtitle.
the easiest way center map on user's location set mapview.usertrackingmode = .follow
(mglusertrackingmodefollow
in objective c). automatically move map when location available.
the reason why you're seeing bogus numbers mapview.userlocation
user's location typically isn't available yet in viewdidload
. use mapview:didupdateuserlocation:
delegate method notified when user's location becomes available , when updates.
Comments
Post a Comment