使用地理空间搜索

您可以在 Cloud Firestore 中执行地理空间查询,以构建 位置感知服务。例如,您可以查找用户与附近兴趣点之间的距离,并按从近到远的顺序对这些兴趣点进行排序。

版本要求

地理空间搜索功能需要 Firestore 企业版数据库。

准备工作

如需执行地理空间搜索,您必须先 为需要搜索的字段创建地理空间索引 。

如需执行地理空间搜索,请在 search(...) 阶段的 query 参数中使用 geoDistance 表达式。

仅支持小于或等于 (<=) 运算符。距离以米为单位。

例如,以下查询会查找列出的地理坐标 1,000 米范围内的所有餐厅。

Web

firestore.pipeline().collection('restaurants')
  .search({
    query: field('location')
      .geoDistance(new GeoPoint(38.989177, -107.065076))
      .lessThan(1000 /* m */)
  });
iOS
firestore.pipeline().collection("restaurants")
    .search(
        query: Field("location")
            .geoDistance(GeoPoint(latitude: 38.989177, longitude: -107.065076))
            .lessThan(1000)
    )
Android
firestore.pipeline()
        .collection("restaurants")
        .search(new SearchOptions()
                .withQuery(field("location")
                        .geoDistance(new GeoPoint(38.989177, -107.065076))
                        .lessThan(1000 /* meters */)));
Node.js
firestore.pipeline().collection('restaurants')
  .search({
    query: field('location')
      .geoDistance(new GeoPoint(38.989177, -107.065076))
      .lessThan(1000 /* m */)
  });