无法比较didUpdateLocations中的Firebase纬度/经度值

问题描述 投票:-1回答:1

我正在处理一个关于从Firebase获取数据并在我的代码中的其他地方使用该数据的简单问题。下面,我有一个函数,它检索一个位置的纬度和经度,并将它们存储在两个变量locationLatlocationLong中定义并初始设置为0.0。我在viewDidLoad中调用此函数。

然后在didUpdateLocations中访问保存纬度和经度的两个变量,以将用户的位置与从Firebase获取的值的纬度和经度进行比较。

我的问题:didUpdateLocations中的if条件不会一直有效,当它没有时,纬度和经度打印0.0或nil并且它不运行if语句。

我知道observeSingleEvent本质上是异步的,但我不确定如何在我的代码中正确使用Firebase数据,尤其是在didUpdateLocations委托函数中。任何建议都会很棒。

以下是与此问题相关的两个功能。第一个是我的函数,它从Firebase中检索纬度和经度。第二个函数是我的didUpdateLocations函数,我的bug出现了。

func getLocationCoordinates() {
        let ref = Database.database().reference().child("map").child(self.titleString)
        ref.observeSingleEvent(of: .value) { (snapshot) in
                let nameSnapshots = snapshot.childSnapshot(forPath: "title")
                let val = nameSnapshots.value as! String
                if self.locationName.text == val {
                    let latitude = snapshot.childSnapshot(forPath: "latitude")
                    let longitude = snapshot.childSnapshot(forPath: "longitude")
                    let latValue = latitude.value as! CLLocationDegrees
                    let lonValue = longitude.value as! CLLocationDegrees
                    self.locationLat = latValue
                    self.locationLong = lonValue
                }
            }
        }

这是didUpdateLocations功能

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        manager.desiredAccuracy = kCLLocationAccuracyBest
        let userLocation :CLLocation = locations[0] as CLLocation
        let locationAddress: CLLocation = CLLocation(latitude: locationLat, longitude: locationLong)
        print("loc latitude = \(locationAddress.coordinate.latitude)")
        print("loc longitude = \(locationAddress.coordinate.longitude)")
        let distanceFrom : CLLocationDistance = userLocation.distance(from: locationAddress)
        if 300 >= distanceFrom {
            checkInOutlet.isEnabled = true
            print("we are here")
        } else {
            checkInOutlet.isEnabled = false
            print("not here yet")
        }
        locationManager.stopUpdatingLocation()
    }
ios swift firebase firebase-realtime-database cllocationmanager
1个回答
2
投票

由于您的getLocationCoordinatesdidUpdateLocationsasync调用,您需要等到getLocationCoordinates从firebase服务器给出结果并使用返回值到didUpdateLocations方法。

为此,你可以使用completion封闭。检查下面的示例代码:

func getLocationCoordinates(completion: @escaping (_ lat: CLLocationDegrees, _ lon: CLLocationDegrees)->()) {

    let ref = Database.database().reference().child("map").child(self.titleString)
    ref.observeSingleEvent(of: .value) { (snapshot) in
        let nameSnapshots = snapshot.childSnapshot(forPath: "title")
        let val = nameSnapshots.value as! String
        if self.locationName.text == val {
            let latitude = snapshot.childSnapshot(forPath: "latitude")
            let longitude = snapshot.childSnapshot(forPath: "longitude")
            let latValue = latitude.value as! CLLocationDegrees
            let lonValue = longitude.value as! CLLocationDegrees
            self.locationLat = latValue
            self.locationLong = lonValue

            completion(latValue, lonValue) //this will return your data
        }
    }
}

我用getLocationCoordinates回调修改了你的completion,当completion从firebase服务器获取数据并从该调用返回latlon值时将调用它。

现在你需要在你的didUpdateLocations方法中使用它,如下所示:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    manager.desiredAccuracy = kCLLocationAccuracyBest
    let userLocation :CLLocation = locations[0] as CLLocation

    //call your method like this
    getLocationCoordinates { (lat, lon) -> () in

        //use lat and lon which returned by above method
        let locationAddress: CLLocation = CLLocation(latitude: lat, longitude: lon)
        print("loc latitude = \(locationAddress.coordinate.latitude)")
        print("loc longitude = \(locationAddress.coordinate.longitude)")
        let distanceFrom : CLLocationDistance = userLocation.distance(from: locationAddress)
        if 300 >= distanceFrom {
            self.checkInOutlet.isEnabled = true
            print("we are here")
        } else {
            self.checkInOutlet.isEnabled = false
            print("not here yet")
        }
        self.locationManager.stopUpdatingLocation()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.