ios - What I am doing wrong with my CLLocation? -
i trying make simple app shows: latitude longitude horizontal accuracy altitude vertical accuracy location start
my code is:
@iboutlet weak var latitude: uilabel! @iboutlet weak var longitude: uilabel! @iboutlet weak var horizontalaccuracy: uilabel! @iboutlet weak var altitude: uilabel! @iboutlet weak var verticalaccuracy: uilabel! @iboutlet weak var distance: uilabel! var locationmanager: cllocationmanager = cllocationmanager() var startlocation: cllocation! @ibaction func resetdistance(sender: anyobject) { startlocation = nil } func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { var latestlocation: anyobject = locations[locations.count - 1] latitude.text = string(format: "%.4f", latestlocation.coordinate.latitude) longitude.text = string(format: "%.4f", latestlocation.coordinate.longitude) horizontalaccuracy.text = string(format: "%.4f", latestlocation.horizontalaccuracy) altitude.text = string(format: "%.4f", latestlocation.altitude) verticalaccuracy.text = string(format: "%.4f", latestlocation.verticalaccuracy) if startlocation == nil { startlocation = latestlocation as! cllocation } var distancebetween: cllocationdistance = latestlocation.distancefromlocation(startlocation) distance.text = string(format: "%.2f", distancebetween) } func locationmanager(manager: cllocationmanager!, didfailwitherror error: nserror!) { }
and in viewdidload section:
locationmanager.desiredaccuracy = kcllocationaccuracybest locationmanager.delegate = self locationmanager.requestwheninuseauthorization() locationmanager.startupdatinglocation() startlocation = nil
i keep getting error: on latitude.text line (everything hooked correctly)
fatal error: unexpectedly found nil while unwrapping optional value
(lldb)
how go adding if let statements? can me find problem?
thanks!
welcome swift. can't anyobject. cast down this:
let latestlocation = locations.last as! cllocation
also, "everything hooked correctly", i'm not sure believe that; try doing println(latitude)
make sure.
finally, code not going work:
locationmanager.requestwheninuseauthorization() locationmanager.startupdatinglocation()
the problem authorization request asynchronous. possibly starting update locations before you've got authorization. have wait until have authorization before start asking updates.
Comments
Post a Comment