swift - 如何从 CLLocationManager 传递经度和纬度以使用全局 swift

标签 swift cllocationmanager latitude-longitude

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/

相关文章:

mysql - 将经纬度文本列移动到 'point' 类型的列中

xcode - Adobe Creative SDK 文件 Swift

swift - 我的 VC 没有初始化器

ios - 如何在 iOS7 兼容设备上使用 UIAlertController() 作为全局变量?

iphone - 位置管理器 :didUpdateLocations: always be called several times

ios - 正确调用 swift 中的函数

具有 dispatch_async/asynchronous 方法的 iOS Singleton API 管理器 : what's the implication?

ios - 为什么 CLLocationManager 不传递事件来处理?

mysql - 在 MySQL 数据库中存储纬度/经度时使用的理想数据类型是什么?

latitude-longitude - 国家边界的纬度/经度数据集