swift 基于位置半径过滤Firebase数据库

icomxhvb  于 2022-11-28  发布在  Swift
关注(0)|答案(3)|浏览(166)

我有以下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 & Swift的新手。

3duebb1j

3duebb1j1#

对于JSON,如下所示:

UsersLocation :{
     autoID1 : {....},
     autoID2 : {.....}
           }

试试这个:-

FIRDatabase.database().reference().child("UsersLocation").queryOrdered(byChild: "lat").queryStarting(atValue: 30).queryEnding(atValue: 60).observeSingleEvent(of: .value, with: {(snap) in

        print(snap)

        if let snapDict = snap.value as? [String:AnyObject]{

            for each in snapDict{

                print(each)

            }
        }
    })
}

这将给予你一个autoID键的所有用户,他们的纬度在30和60之间,你检索数据的顺序将是随机的,所以以升序遍历他们,你可以排序你收到的字典.

cyej8jka

cyej8jka2#

下面是一个你可以尝试的解决方案。这假设你已经把所有的帖子从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 }
       }
    }
}
tquggr8v

tquggr8v3#

使用GeoHash
这里是它的完整文档,您可以使用单个https://firebase.google.com/docs/firestore/solutions/geoqueries
将此添加到您的播客文件pod 'GeoFire/Utils'
插入GeoHash

let latitude = 51.5074
let longitude = 0.12780
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

let hash = GFUtils.geoHash(forLocation: location)

// Add the hash and the lat/lng to the document. We will use the hash
// for queries and the lat/lng for distance comparisons.
let documentData: [String: Any] = [
    "geohash": hash,
    "lat": latitude,
    "lng": longitude
]

let londonRef = db.collection("cities").document("LON")
londonRef.updateData(documentData) { error in
    // ...
}

获取筛选器数据

let queries = queryBounds.map { bound -> Query in
    return db.collection("cities")
        .order(by: "geohash")
        .start(at: [bound.startValue])
        .end(at: [bound.endValue])
}

相关问题