代码之家  ›  专栏  ›  技术社区  ›  lampShadesDrifter

如何使用函数过滤Firestore采集数据(用于位置距离过滤)

  •  0
  • lampShadesDrifter  · 技术社区  · 6 年前

    想知道如何使用函数筛选Firestore文档集合,其中一些函数参数是集合文档的值。

    {
      pointOfInterest: "Some string label"
      longitude: -100.123
      latitude: 50.456
    }
    

    const getCurrentLatLong = () => {
        // do some stuff, then...
        return { latitude: someNumber, longitude: someOtherNumber }
    }
    

    let currentLocation = getCurrentLatLong()
    let filteredSet = firebase
        .firestore()
        .collection('MyCollection')
        // filtering each individual document 
        .filter(function (document, currentLocation) {
             let docLat = document.latitude
             let docLong = document.longitude
             return distance(
                 {latitude: docLat, longitude: docLong},
                 currentLocation) 
                 < SOME_CONST_DISTANCE
         })
    

    因此,最终会得到集合中所有文档的某个筛选器集,这些文档与当前位置的距离小于某个常量距离。

    一些谷歌搜索导致了一些潜在的起点( https://stackoverflow.com/a/45359952/8236733 https://firebase.google.com/docs/reference/js/firebase.database.Query#on ),但不太确定如何使用链接中显示的内容。任何关于如何实现这一目标的建议或文件都将不胜感激。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Frank van Puffelen    6 年前

    没有办法将函数传递到CloudFireStore,然后用它过滤返回的文档。您要么需要传入静态值(例如,您想要返回的纬度和经度),要么需要读取所有文档并使用客户端函数进行过滤。

    geographical point data type . 您可以自己构建它,尽管您应该意识到目前这种查询效率很低。

        2
  •  1
  •   Martin Bramwell    6 年前

    https://github.com/mbramwell1/GeoFire-Android

    这使您可以执行标准的Firestore查询,但也增加了按位置搜索的功能。看一看,它可以做你想做的事。