繁体   English   中英

Flutter Firestore Geo 查询距离我的位置一定的距离

[英]Flutter Firestore Geo query for certain distance from my location

我在我的 firebase 数据库中存储我的用户数据,包括他们的实时位置作为GeoPoint数据类型。 现在我需要从与我的位置有一定距离的数据库中选择和获取用户。 我使用这行代码来检索我的位置:

position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

不,我需要在 flutter 中使用 firebase 查询来检索我周围 500 米的用户。这意味着我应该检索距离 firebase 数据库 500 米的所有用户。 我怎样才能做到这一点?

最后我找到了解决方案:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM