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

时间:2017-11-06 03:28:15

标签: swift geofire

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

非常感谢。

1 个答案:

答案 0 :(得分:2)

首先尝试使用如下的cocopod,

pod 'Firebase', '~>4.0'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
pod 'GeoFire', :git => 'https://github.com/firebase/geofire-objc.git'

使用以下代码设置并从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
}
如果您发现此错误,请

' FirebaseDatabase / FirebaseDatabase.h'找不到文件。

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

image gefire with firebase

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

A sample in firebase after storing location is shown below.