皈依舞
更新:Firestore当前不支持实际的GeoPoint查询,因此尽管以下查询成功执行,但它仅按纬度而不是经度进行过滤,因此将返回许多附近的结果。最好的解决方案是使用geohash。要了解自己如何做类似的事情,请观看此视频。这可以通过创建一个小于查询的边界框来完成。至于效率,我不能说。请注意,应检查纬度/经度偏移〜1英里的精度,但这是一种快速的方法:SWIFT 3.0版本func getDocumentNearBy(latitude: Double, longitude: Double, distance: Double) { // ~1 mile of lat and lon in degrees let lat = 0.0144927536231884 let lon = 0.0181818181818182 let lowerLat = latitude - (lat * distance) let lowerLon = longitude - (lon * distance) let greaterLat = latitude + (lat * distance) let greaterLon = longitude + (lon * distance) let lesserGeopoint = GeoPoint(latitude: lowerLat, longitude: lowerLon) let greaterGeopoint = GeoPoint(latitude: greaterLat, longitude: greaterLon) let docRef = Firestore.firestore().collection("locations") let query = docRef.whereField("location", isGreaterThan: lesserGeopoint).whereField("location", isLessThan: greaterGeopoint) query.getDocuments { snapshot, error in if let error = error { print("Error getting documents: \(error)") } else { for document in snapshot!.documents { print("\(document.documentID) => \(document.data())") } } }}func run() { // Get all locations within 10 miles of Google Headquarters getDocumentNearBy(latitude: 37.422000, longitude: -122.084057, distance: 10)}