在我的應用程式中,人們可以在地圖上放置標記(使用google_maps_flutter)。這些標記是公開的。當其他人打開地圖時,他們也可以看到標記。
問題是,這會超時導致大量讀取,因為當您打開地圖時,我的應用程式將從Firestore資料庫中讀取每個標記。所以我正在嘗試實作一些延遲加載系統。
然而,事實證明這比我想象的要困難得多。
這就是我所擁有的:
// get all markers within a certain radius
Future<Set<Marker>> getMarkersWithinRadius(LatLng center, double radius) async {
final Set<Marker> markers = {};
// only get markers within a certain radius
final QuerySnapshot<Map<String, dynamic>> querySnapshot = await markersFirestoreReference
.where('latitude', isGreaterThanOrEqualTo: center.latitude - radius)
.where('latitude', isLessThanOrEqualTo: center.latitude radius)
.where('longitude', isGreaterThanOrEqualTo: center.longitude - radius)
.where('longitude', isLessThanOrEqualTo: center.longitude radius)
.get();
// loop through all docs and add them to the markers set
for (final QueryDocumentSnapshot<Map<String, dynamic>> doc in querySnapshot.docs) {
final MarkerId markerId = MarkerId(doc.data()['markerId']);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(
doc.data()['latitude'],
doc.data()['longitude'],
),
infoWindow: InfoWindow(title: markerId.value.toString(), snippet: '*'),
onTap: () {
//Navigator.pushNamed(context, '/markerView',
// arguments: markerId.value);
},
);
markers.add(marker);
}
return markers;
}
這不起作用,因為Firebase目前顯然不支持在單個請求中查詢多個欄位。
uj5u.com熱心網友回復:
您感興趣的是使用空間索引來查詢 Firestore。您可能想使用geofire存盤資料。Geofire 是一種確定性地確定資料的索引值,然后通過將地球劃分為幾個較小的網格來查詢該索引的方法。然后,您可以使用地理查詢在某個位置進行查詢,然后檢索有關該位置的實時更新。這會從中心位置進行半徑查詢,我的建議是隨著地圖比例的變化,將這些查詢的半徑更新為大于您當前的地圖視圖。此外,雖然這種空間索引對于knn 查詢效果很好在正常位置,當您向兩極移動時,通過穿過兩極來調整更接近的位置會出現問題。
額外檔案:
GeoFire 安卓檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/519345.html
標籤:Google Cloud Collective 扑火力基地镖谷歌地图谷歌云火库
上一篇:京東云開發者| Redis資料結構(二)-List、Hash、Set及Sorted Set的結構實作
下一篇:java.lang.NullPointerException:嘗試在空物件參考上呼叫虛擬方法“voidcom.google.android.gms.maps.GoogleMap.clear()”
