如何从 CLLocationManager 传递经度和纬度以全局使用 swift
how to pass longitude and latitude from CLLocationManager to use globally swift
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 定义开始的地方声明它:
class MyClass {
private var currentCoordinate: CLLocationCoordinate2D?
}
然后在您的位置委托方法中分配当前值:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
currentCoordinate = manager.location.coordinate
}
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 定义开始的地方声明它:
class MyClass {
private var currentCoordinate: CLLocationCoordinate2D?
}
然后在您的位置委托方法中分配当前值:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
currentCoordinate = manager.location.coordinate
}