2016-09-22 26 views
0

我正在使用Firebase Db,并且想要保存设备在Firebase上的经纬度值。我来自的LocationManager有两个值()Swift - 如何获取功能外的经度和纬度值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) 
    } 

接下来,我尝试纬度保存,长期在火力地堡:

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

Error: "Use of Unresolved Identifier".

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

回答

2

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

class YourClass { 
    private var currentCoordinate: CLLocationCoordinate2D? 
} 

而在你的位置,委托方法分配的电流值

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