【问题标题】:Flutter Firestore Geo query for certain distance from my locationFlutter Firestore 地理查询距我的位置一定距离
【发布时间】:2021-12-04 23:43:49
【问题描述】:
我将我的用户数据(包括他们的实时位置)作为 GeoPoint 数据类型存储在我的 firebase 数据库中。
现在我需要从距我的位置一定距离的数据库中选择并获取用户。
我使用这行代码来检索我的位置:
position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
不,我需要在 Flutter 中使用 Firebase 查询来检索我周围 500 米的用户。这意味着我应该从 Firebase 数据库中检索距离为 500 米的所有用户。
我该怎么做?
【问题讨论】:
标签:
firebase
flutter
google-cloud-functions
geolocation
【解决方案1】:
终于找到了解决办法:
position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
await getGenderIcons(context);
QuerySnapshot querySnapshot;
double lat = 0.0144927536231884;
double lon = 0.0181818181818182;
double distance = 1000*0.000621371;
double lowerLat = position.latitude - (lat * distance);
double lowerLon = position.longitude - (lon * distance);
double greaterLat = position.latitude + (lat * distance);
double greaterLon = position.longitude + (lon * distance);
GeoPoint lesserGeopoint = GeoPoint(lowerLat,lowerLon);
GeoPoint greaterGeopoint = GeoPoint(greaterLat,greaterLon);
querySnapshot = await usersRef
.where("livelocation", isGreaterThan: lesserGeopoint)
.where("livelocation", isLessThan: greaterGeopoint)
.limit(100)
.get();