在Android上使用Algolia获取我的位置和获取记录的位置之间的距离

Tho*_*med -3 android google-maps firebase algolia

我正在使用Algolia的InstantSearch Android库.这是我的Query:

searcher.setQuery(new Query().setAroundLatLng(new AbstractQuery.LatLng(lat, lng)).setAroundRadius(5000)).
Run Code Online (Sandbox Code Playgroud)

如何显示获取的记录与当前位置之间的距离?

PLN*_*ech 6

建立在@ Raphi的最佳答案之上,以下是使用InstantSearch Android显示距离的方法:您只需编写自定义命中View.

  • 创建自定义点击视图:这里是一个专门的TextView实现AlgoliaHitView
  • onUpdateView,获得_rankingInfo.matchedGeoLocation.distanceRaphi推荐
  • 在视图中使用此值,例如 setText(distance + " meters away.")


小智 5

实际上,您可以通过将参数添加getRankingInfo=true到搜索查询中来查找响应中每个匹配的距离.

我不熟悉InstantSearch Android,但如果您可以访问原始响应,那么请查看_rankingInfo.matchedGeoLocation.distance您的记录:

{
  [...] // your record fields
  "_rankingInfo": {
    "nbTypos": 0,
    "firstMatchedWord": 0,
    "proximityDistance": 0,
    "userScore": 11,
    "geoDistance": 468,
    "geoPrecision": 1,
    "nbExactWords": 1,
    "words": 1,
    "filters": 0,
    "matchedGeoLocation": {
      "lat": 48.86,
      "lng": 2.3443,
      "distance": 468
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请参阅https://www.algolia.com/doc/guides/searching/geo-search/?language=rails#identifying-the-matching-geo-search-with-rankinginfo