【问题标题】:Swift - How to get latitude and longitude value outside function LocationManager()Swift - 如何在函数 LocationManager() 之外获取纬度和经度值
【发布时间】:2016-09-22 11:46:24
【问题描述】:

我正在使用 Firebase Db,我想在 Firebase 上保存设备的经纬度值。我有两个来自 locationManager() 的值

这是我的函数 locationManager()

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       let lastLocation: CLLocation = locations[locations.count - 1]
       let lat = String(format: "%.6f", lastLocation.coordinate.latitude)
       let long = String(format: "%.6f", lastLocation.coordinate.longitude)
    }

接下来,我尝试在 Firebase 上保存 lat,long:

 ref = FIRDatabase.database().reference()
    ref.setValue(lat)

错误:“使用未解析的标识符”。

如何在函数LocationManager()之外获取经纬度值。

【问题讨论】:

    标签: swift firebase core-location locationmanager


    【解决方案1】:

    您可以在类定义的位置声明CLLocationManagerDelegate

    class YourClass {
        private var currentCoordinate: CLLocationCoordinate2D?
    }
    

    并且在您的位置委托方法中分配当前值

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        currentCoordinate = manager.location.coordinate
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-04
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2023-03-21
      • 2016-09-15
      相关资源
      最近更新 更多