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 }
       }
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...