Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 根据位置半径过滤Firebase数据库大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下Firebase数据库结构,

"-KSupX7CppMD1zbqIy0y" : {
  "addedByUser" : "zQpb7o18VzYsSoTQtT9DNhOqTUn2","content" : "Post 1","cost" : "20","duration" : "Weekly","latitude" : "40.7594479995956","longitude" : "-73.9838934062393","timestamp" : "Fri 30 Sep"
},"-KSuphuqO6a0lnrJYUkt" : {
  "addedByUser" : "zQpb7o18VzYsSoTQtT9DNhOqTUn2","content" : "Post 2","cost" : "10","duration" : "Daily","latitude" : "40.7594329996462","longitude" : "-73.9846261181847","timestamp" : "Fri 30 Sep"
}

我需要在一定的距离参数(小于50米,100米,150米和200米)内过滤帖子.我可以从“经度”和“经度”获得坐标. “纬度”,但我正在努力过滤帖子.任何帮助将不胜感激.

提前谢谢了.新手到Firebase&迅速.

解决方法

以下是您可以尝试的解决方案.这假设您已将所有帖子从Firebase下载到应用程序并存储在数组中.你也可以虑使用我没有亲自使用的GeoFire(但我想).我相信它允许您按位置查询Firebase,因此您不必首先将所有帖子下载到用户的设备并在客户端进行过滤.

// assumes you have set up the app to get the user's current LOCATIOn
var currentLOCATIOn: CLLOCATIOn?

// Your array of posts downloaded from Firebase
var postArray = [post]()

let metersInTwentyFiveMiles = 40233.6

// You Could put this in viewDidAppear after you load the post array from Firebase
for post in postArray {

    var distanceInMeters: CLLOCATIOnDistance = 0

    let postLOCATIOn = CLLOCATIOn(latitude: post.latitude,longitude: post.longitudE)


    if let currentLOCATIOn = self.currentLOCATIOn {
         // Set distanceInMeters to the distance between the user's current LOCATIOn and the LOCATIOn of the post.
         distanceInMeters = currentLOCATIOn.distanceFromLOCATIOn(postLOCATIOn)
         // If this distance is not within 25 miles,remove this post from the array
         if self.metersInTwentyFiveMiles < distanceInMeters {
            self.posts = self.posts.filter{ $0 != post }
       }
    }
}
@H_673_26@

大佬总结

以上是大佬教程为你收集整理的swift – 根据位置半径过滤Firebase数据库全部内容,希望文章能够帮你解决swift – 根据位置半径过滤Firebase数据库所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。