昨天我在堆栈溢出中问了一个关于如何在消息正文中嵌入我当前位置的问题
我得到了答案,我更正了我的代码
我的代码在控制台中运行良好,但我不知道如何从 displayLocationInfo 函数中获取数据并将它们存储在变量中;然后将变量放入消息正文中。
我是 iPhone 开发的新手
有什么帮助吗?
class Location: NSObject,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentLocation : CLPlacemark?
var detectLocation : CLLocationManager?
var locManager = CLLocationManager()
func viewDidLoad()
{
viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func updateLocation()
{
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
print("Error: " + error!.localizedDescription)
return
}
if placemarks!.count > 0
{
let pm = placemarks![0]
self.displayLocationInfo(pm)
}
else
{
print("Error with the data.")
}
})
}
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error: " + error.localizedDescription)
}
}