gpt4 book ai didi

swift - 过滤Firebase数据[iOS]

转载 作者:行者123 更新时间:2023-11-30 11:45:38 25 4
gpt4 key购买 nike

我的Firebase具有这种配置。 https://screenshots.firefox.com/s0UNlw3IOKEeFCCr/console.firebase.google.com

从iOS,我试图获取所有经度和纬度在一定范围内的数据。是否可以从firebase进行过滤,或者我必须下载所有数据,然后在iOS中进行过滤。

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let rect = mapView.visibleMapRect
let neMapPoint = MKMapPointMake(MKMapRectGetMaxX(rect), rect.origin.y)
let swMapPoint = MKMapPointMake(rect.origin.x, MKMapRectGetMaxY(rect))
let neCoord = MKCoordinateForMapPoint(neMapPoint)
let swCoord = MKCoordinateForMapPoint(swMapPoint)

getData(neCoord, swCoord)
}

private func getData(_ ne: CLLocationCoordinate2D, _ sw: CLLocationCoordinate2D) {
let ref = Database.database().reference().child("posts")

}


我想获取所有帖子项,这些项的纬度小于ne纬度,比sw纬度小得多,经度相同。谢谢。

最佳答案

您将创建一个Post对象。就像是:

struct Post {
let latitude: String
let longitude: String
init(dictionary: [String: Any]) {
self.latitude = dictionary["latitude"] as? String ?? ""
self.longitude = dictionary["longitude"] as? String ?? ""
}
}


然后在文件中创建一个帖子数组:

var posts: [Post]()
private func getData(_ ne: CLLocationCoordinate2D, _ sw:
CLLocationCoordinate2D) {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else {return}
dictionaries.forEach(key, value) {
guard let dictionary = value as? [String: Any] else {return}
guard let latitude = dictionary["latitude"] else {return}
guard let longitude = dictionary["longitude"] else {return}

//check if latitude is less than ne latitude and gretaer than sw lattitude and the same for longitude, then append to array.

let post = Post(dictionary: dictionary)
posts.append(post)

}
})
}) { (err) in
print(err)
}
}


然后,您将得到一个数组,其中包含具有正确的经度和纬度的帖子。这是一个粗略的想法。

关于swift - 过滤Firebase数据[iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850968/

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