简体   繁体   English

Flutter & firebase 通过 geohash 获取文档

[英]Flutter & firebase get docs by geohash

I'm trying to get a documents based on a geohash upper and lower bounds.我正在尝试获取基于 geohash 上限和下限的文档。 'Location A' is the location of a user, and we're trying to find other locations that are within the bounds of 'Location A' in firebase. “位置 A”是用户的位置,我们正在尝试查找 firebase 中“位置 A”范围内的其他位置。 I was following this tutorial我正在关注本教程

https://levelup.gitconnected.com/nearby-location-queries-with-cloud-firestore-e7f4a2f18f9d https://levelup.gitconnected.com/nearby-location-queries-with-cloud-firestore-e7f4a2f18f9d

and checking with this website并与本网站核对

https://www.movable-type.co.uk/scripts/geohash.html https://www.movable-type.co.uk/scripts/geohash.html

The coordinates of 'location A' are 52.462570419161594, -2.0120335164758725 and the coordinates of 'Location B' are 52.46648448588268, -1.9841125656313279 . '位置 A' 的坐标是52.462570419161594, -2.0120335164758725和'位置 B' 的坐标是52.46648448588268, -1.9841125656313279 These are very close to eachother so, with the code below i'd assume that with the firebase query it'd return 'Location B' when querying from 'Location A's geohash but this isn't happening and it's because the getGeohashRange function is returning the wrong value but i'm not sure what i'm doing wrong这些彼此非常接近,因此,使用下面的代码,我假设使用 firebase 查询时,从“位置 A”的 geohash 查询时它会返回“位置 B”,但这并没有发生,这是因为getGeohashRange ZC1C425268E68385D1AB5074C17A94F1 返回错误的值,但我不确定我做错了什么

Location A geohash = 'gcqd6' Location B geohash = 'gcqd6'位置 A geohash = 'gcqd6' 位置 B geohash = 'gcqd6'

The upper & lower that the getGeohashRange is returning = lower: "mpt5mf52n18z" upper: "mptmvc2wncyc" getGeohashRange返回的上限和下限 = 下限:“mpt5mf52n18z”上限:“mptmvc2wncyc”

This is the code & firebase query这是代码 & firebase 查询

// Get the geohash upper & lower bounds
GeoHashRange getGeohashRange({
  required double latitude,
  required double longitude,
  double distance = 12, // miles
}) {
  double lat = 0.0144927536231884; // degrees latitude per mile
  double lon = 0.0181818181818182; // degrees longitude per mile

  double lowerLat = latitude - lat * distance;
  double lowerLon = longitude - lon * distance;

  double upperLat = latitude + lat * distance;
  double upperLon = longitude + lon * distance;

  GeoHasher geo = GeoHasher();

  var lower = geo.encode(lowerLat, lowerLon);
  var upper = geo.encode(upperLat, upperLon);

  return GeoHashRange(lower: lower, upper: upper);
}

class GeoHashRange {
  late String upper;
  late String lower;

  GeoHashRange({
    required this.upper,
    required this.lower,
  })
}



// Query firebase for locations
Stream<List<Garage>> getLocalGarages(Position position) {
  GeoHashRange range = getGeohashRange(
    latitude: position.latitude,
    longitude: position.longitude,
  );

  var ref = _db
      .collection('garages')
      .where("geohash", isGreaterThan: range.upper)
      .where("geohash", isLessThan: range.lower);

  return ref.snapshots().map(
        (list) =>
            list.docs.map((doc) => Garage.fromJson(doc.data())).toList(),
      );
}

Turns out theres a plugin that can actually help with this.原来有一个插件可以真正帮助解决这个问题。 This doc has some info https://firebase.google.com/docs/firestore/solutions/geoqueries#java and this is the plugin https://pub.dev/packages/geoflutterfire这个文档有一些信息https://firebase.google.com/docs/firestore/solutions/geoqueries#java这是插件https://pub.dev/packages/geoflutterfire

This is my new code这是我的新代码

Stream<List<Garage>> localGarages(Position position) {
  var ref = _db.collection('garages');

  GeoFirePoint center =
      geo.point(latitude: position.latitude, longitude: position.longitude);
  double radiusInKM = 20;

  return geo
      .collection(collectionRef: ref)
      .within(center: center, radius: radiusInKM, field: 'position')
      .map((x) => x.map((e) => Garage.fromJson(e.data()!)).toList());
}

There is an example of the 'position' field in the doc above上面的文档中有一个“位置”字段的示例

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

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