过滤firebase数据[iOS]

时间:2018-02-18 11:08:02

标签: swift firebase

我的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纬度和gretaer比sw lattitude和经度相同。感谢。

1 个答案:

答案 0 :(得分:0)

您将创建一个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 ?? ""
    }
}

然后在您的文件中创建一个post数组:

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

然后你有一个数组填充了具有正确经度和纬度的帖子。这是一个粗略的想法。