ios - CLLocationManager: Unexpectedly Found Nil While Unwrapping an Optional Value -
here's code in viewcontroller.swift:
import uikit import alamofire import swiftyjson import mapkit import corelocation import twitterkit class viewcontroller: uiviewcontroller, mkmapviewdelegate, cllocationmanagerdelegate { @iboutlet weak var mymap: mkmapview! @iboutlet weak var counterlabel: uilabel! var manager = cllocationmanager() override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. //calling cllocation manager.delegate = self manager.desiredaccuracy = kcllocationaccuracybest manager.requestwheninuseauthorization() manager.startupdatinglocation() //map region let locationzoom = self.manager.location var latitude: double = locationzoom.coordinate.latitude var longitude: double = locationzoom.coordinate.longitude var centerlatitude: cllocationdegrees = locationzoom.coordinate.latitude var centerlongitude: cllocationdegrees = locationzoom.coordinate.longitude var latdelta: cllocationdegrees = 0.03 var longdelta: cllocationdegrees = 0.03 var span: mkcoordinatespan = mkcoordinatespanmake(latdelta, longdelta) var location: cllocationcoordinate2d = cllocationcoordinate2dmake(centerlatitude, centerlongitude) var region: mkcoordinateregion = mkcoordinateregionmake(location, span) // creates map region center mymap.setregion(region, animated: true) } @ibaction func zoom(sender: anyobject) { let locationzoom = self.manager.location var latitude: double = locationzoom.coordinate.latitude var longitude: double = locationzoom.coordinate.longitude var centerlatitude:cllocationdegrees = locationzoom.coordinate.latitude var centerlongitude: cllocationdegrees = locationzoom.coordinate.longitude var latdelta:cllocationdegrees = 0.03 var longdelta:cllocationdegrees = 0.03 var span:mkcoordinatespan = mkcoordinatespanmake(latdelta, longdelta) var location:cllocationcoordinate2d = cllocationcoordinate2dmake(centerlatitude, centerlongitude) var region:mkcoordinateregion = mkcoordinateregionmake(location, span) // creates map region center mymap.setregion(region, animated: true) } }
now, error described thus:
fatal error: unexpectedly found nil while unwrapping optional value
thread 1 pointing towards being issue:
var latitude: double = locationzoom.coordinate.latitude
however, when testing on simulator, have found issue occur longitude variable. still, not understand why values both longitude , latitude set "nil." have set cllocationmanager correctly, made sure modify info.plist necessary include nslocationwheninuseusagedescription
, nslocationalwaysusagedescription
, etc. what's going on, , how correct issue?
this happens because when view loads has not received location update. implement didupdatelocations , access location info there. if need reference can didupdatelocations
func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { let locationzoom = locations.last as! cllocation let latitude: double = locationzoom.coordinate.latitude let longitude: double = locationzoom.coordinate.longitude println(latitude) println(longitude) }
Comments
Post a Comment