func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
println("locations = \(locValue.latitude) \(locValue.longitude)")
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.locationManager.stopUpdatingLocation()
//self.dismissViewControllerAnimated(true, completion: nil)
self.displayLocationInfo(pm)
} else {
println("Error with the data.")
}
})
}
目前我能够通过坐标获取当前位置并且能够打印它。但是如何将函数中的值发送到 Web 服务呢?我发现该变量在函数外部不可访问。谁能帮忙?非常感谢!
最佳答案
如果你想在 CLLocationManagerDelegate
方法之外访问你的位置变量,你应该在你的类定义开始的地方声明它:
class MyClass {
private var currentCoordinate: CLLocationCoordinate2D?
}
然后在您的位置委托(delegate)方法中分配当前值:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
currentCoordinate = manager.location.coordinate
}
关于swift - 如何从 CLLocationManager 传递经度和纬度以使用全局 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30211360/