如何使用 Geofire 与 Swift 来存储一个模拟器的位置,然后在另一个模拟器中显示该位置。

Slo*_*oan 0 swift geofire

我正在使用 Swift 构建一个 iOS 来共享位置。有谁知道如何使用 Geofire 与 Swift 来存储一个模拟器中的位置,然后在另一个模拟器中显示存储的位置?

多谢。

Lyj*_*son 5

首先尝试使用 cocopods,如下所示,

pod 'Firebase', '~>4.0'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
pod 'GeoFire', :git => 'https://github.com/firebase/geofire-objc.git'
Run Code Online (Sandbox Code Playgroud)

使用以下代码从 Geofire 设置和获取位置。

struct FndDatabase {
    static let LOCATION = Database.database().reference().child("userlocation")
    static let GEO_REF = GeoFire(firebaseRef: LOCATION)
}


func setUserLocation(location: CLLocation) {
    let user = Auth.auth().currentUser?.uid //userid for the logged in user
    if let uid = user {
        FndDatabase.GEO_REF?.setLocation(location, forKey: uid)
    }
}

func getLocation() -> CLLocation? {
    var loc: CLLocation?
    if let uid = Auth.auth().currentUser?.uid {
        FndDatabase.GEO_REF?.getLocationForKey(uid, withCallback: { (location, err) in
            if err != nil {
                print( "Error getting Userlocation from Geofire \(err.debugDescription)")
            } else {
                print( "UserLocation is available \(location.debugDescription)")
                loc = location
            }
        })

    }
    return loc
}
Run Code Online (Sandbox Code Playgroud)

如果您看到此错误 “FirebaseDatabase/FirebaseDatabase.h”文件未找到。

然后执行此操作,从 pod 中选择 FirebaseDatabase.framework 并将其链接到 Geofire,如图所示

图像 gefire 与 firebase

示例代码上传至: https: //github.com/ledwinson/firebasegeo

存储位置后 Firebase 中的示例如下所示。