3 回答

TA貢獻1851條經(jīng)驗 獲得超3個贊
更新:Firestore當前不支持實際的GeoPoint查詢,因此盡管以下查詢成功執(zhí)行,但它僅按緯度而不是經(jīng)度進行過濾,因此將返回許多附近的結(jié)果。最好的解決方案是使用geohash。要了解自己如何做類似的事情,請觀看此視頻。
這可以通過創(chuàng)建一個小于查詢的邊界框來完成。至于效率,我不能說。
請注意,應(yīng)檢查緯度/經(jīng)度偏移?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)
}

TA貢獻1811條經(jīng)驗 獲得超5個贊
自@monkeybonkey首次提出此問題以來,已經(jīng)引入了一個新項目。該項目稱為GEOFirestore。
使用此庫,您可以在一個圓圈內(nèi)執(zhí)行查詢,例如查詢文檔:
const geoQuery = geoFirestore.query({
center: new firebase.firestore.GeoPoint(10.38, 2.41),
radius: 10.5
});
您可以通過npm安裝GeoFirestore。您將必須單獨安裝Firebase(因為它是GeoFirestore的對等依賴項):
$ npm install geofirestore firebase --save
添加回答
舉報