swift - Updating Current Location -
let locationmanager = cllocationmanager() @iboutlet var map: mkmapview! func getlocation ()-> cllocation! { self.locationmanager.startupdatinglocation() sleep(1) self.locationmanager.stopupdatinglocation() var x = locationmanager.location return x } override func viewdidload() { super.viewdidload() self.locationmanager.desiredaccuracy = kcllocationaccuracybest self.locationmanager.requestwheninuseauthorization() var mapcoord:cllocation! = getlocation() var maplat:double! = mapcoord.coordinate.latitude //where thread error var maplong:double! = mapcoord.coordinate.longitude //setup of rest of map completed
my code above, in swift. receiving error:
thread error of unexpected nil value
while calling coordinates getlocation()
function. changed both maplat
, maplong
test causing nil. app load receive message in output saying current location cannot updated have no asked permission; however, have function request permission in beginning of viewdidload()
. have used format before on different project , has worked, know must missing obvious, etc. thank help.
your code wrong, , won't work.
you can't write procedural code starts location updates, pauses, stops location updates, , gets location.
you have set location manager's delegate, start location updates, , return.
at point after that, system call locationmanager:didupdatelocations
method.
then need check accuracy of location , make sure it's accurate enough. (frequently first several location readings when gps first powered quite bad, , don't meet desired accuracy. suggest horizontal accuracy of 200 m or so, depending on app's requirements.
if reading accurate enough can turn off updates , use location needs.
p.s. never, ever, ever use sleep()
function on main thread. in fact, novice ios mac os developer, should pretend sleep function doesn't exist.
Comments
Post a Comment