Swift:在展开Optional值时为零。在尝试获取用户位置时

时间:2014-12-10 22:59:50

标签: swift ios8 coordinates core-location cllocationmanager

所以我是应用程序开发的新手,我试图了解用户LocationManager的工作原理。我此时的目标是“请求授权”#34; >检查它是否已获得授权" > if true {获取变量中的位置和商店坐标}

这是我的代码:

let locationManager = CLLocationManager()

var coordinates: String?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

aivRefresh.hidden = true

self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {


  locationManager.delegate = self
  locationManager.desiredAccuracy = kCLLocationAccuracyBest
  locationManager.startUpdatingLocation()
 }

}



func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){



if locations != nil {

  var location:CLLocationCoordinate2D = manager.location.coordinate

  coordinates = "\(location.longitude),\(location.latitude)"
}

0 个答案:

没有答案