gpt4 book ai didi

swift - 使用 Swift 的 GeoFire

转载 作者:可可西里 更新时间:2023-11-01 02:03:58 24 4
gpt4 key购买 nike

我计划使用 GeoFire 根据位置过滤我的 firebase 数据。不知道如何开始,因为我刚刚开始开发。我在一个类中创建了一张图片和一个标题等:

    @IBAction func shareDidTap(_ sender: Any) {
if let image = image, let caption = textView.text {
let newMedia = Media(type: "image", caption: caption, createdBy: user, image: image)
newMedia.save(completion: { (error) in
if let error = error {
self.alert(title: "Oops!", message: error.localizedDescription, buttonTitle: "OK")
} else {
self.user.share(newMedia: newMedia)

在单独的“用户”类 (MVC) 中,我将其保存到 Firebase 中

    func share(newMedia: Media) {
DatabaseReference.users(uid: uid).reference().child("media").childByAutoId().setValue(newMedia.uid)
}

我在哪里实例化 GeoFire 引用?我是否必须使用 Core Location 来获取我自己的纬度和经度?任何帮助,将不胜感激。谢谢你。

最佳答案

添加GeoFire

使用 pod 安装pod 'GeoFire', :git => 'https://github.com/firebase/geofire-objc.git'

然后导入到项目import GeoFire

使用

创建对它的引用
let rootRef = Database.database().reference()
let geoRef = GeoFire(firebaseRef: rootRef.child("user_locations"))

要获取当前用户位置,您可以通过 cocoa pods 使用位置管理器:https://github.com/varshylmobile/LocationManager

我猜您希望保存在数据库中的媒体有一个位置。然后,您使用 GeoFire 使用 GeoFire.setlocation 保存位置当您将帖子保存到数据库时调用此方法,GeoFire 会处理使用您的帖子 ID 添加位置的数据库结构。

一个例子:

geoFire!.setLocation(myLocation, forKey: userID) { (error) in
if (error != nil) {
debugPrint("An error occured: \(error)")
} else {
print("Saved location successfully!")
}
}

然后您可以使用 CircleQuery 查询位置数据库您可以给出一个半径,表示您想从给定位置(用户位置)

查询数据库的距离

一个例子:

let query = geoRef.query(at: userLocation, withRadius: 1000)

query?.observe(.keyEntered, with: { key, location in
guard let key = key else { return }
print("Key: " + key + "entered the search radius.")
})

我使用的一个非常好的教程是: https://www.youtube.com/watch?v=wr19iKENC-k&t=600s

查看他们的 channel 并查询 GeoFire 以查找可能对您有进一步帮助的其他视频。

关于swift - 使用 Swift 的 GeoFire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44611121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com