【问题标题】:Reverse geocoding query with Algolia places.js library使用 Algolia places.js 库进行反向地理编码查询
【发布时间】:2019-05-17 01:43:22
【问题描述】:

我正在使用places.js 库进行自动完成,但也想将其用于反向地理编码。根据https://community.algolia.com/places/examples.html#reverse-geocoding,这应该是可能的 - 自动完成工作正常,但不是反向调用。代码摘录:

            places.configure({
                hitsPerPage: 1,
                aroundLatLng: position.coords.latitude + ',' + position.coords.longitude,
            });
            places.reverse({
                //aroundLatLng: position.coords.latitude + ',' + position.coords.longitude,
                //hitsPerPage: 1
            }).then(function(response){
                var hits = response.hits;
                var suggestion = hits[0];
                if (suggestion && (suggestion.locale_names || suggestion.city)) {
                    address_input.value = suggestion.locale_names.default[0] || suggestion.city.default[0];
                }
            });

这会触发对正确端点的调用,但缺少 aroundLatLng 的错误。我已经确认数据在那里 - 而且hitsPerPage 仍然是默认值 5。 正如您从注释行中看到的那样,我尝试将选项直接传递给reverse 调用,并使用configure

谁能帮忙告诉我使用places.js库进行反向调用的正确方法吗?

谢谢。

【问题讨论】:

  • 你确定position.coords.{latitude|longitude}中有实际数据吗?
  • 是的 100%。我正在做的事情根本上是错误的,因为当我查看名为的 URL 时,甚至 hitsPerPage 值也没有正确传递

标签: javascript algolia


【解决方案1】:

所以问题在于 Algolia 如何提供两种调用反向地理编码的方法,但实际上只记录了一种。

Algolia 的反向地理编码示例使用 algolia 客户端 库中的位置实现,它允许您执行以下操作:

const places = algoliasearch.initPlaces('appId', 'apiKey');
places.reverse({ aroundLatLng: 'lat,lng', hitsPerPage: 5 }).then(callback);

places.js 库中还有另一个 reverse 方法可用,这个方法的参数略有不同:

const placesAutocomplete = places({ appId: '', apiKey: '', container: inputEl });
places.reverse('lat,lng', { hitsPerPage: 5 });

注意必须如何将纬度/经度对作为第一个参数给出,然后查询参数作为第二个对象参数。

【讨论】:

  • 太棒了——感谢奥利弗!对于其他发现此问题的人 - 响应也采用不同的格式 - 只是一个结果数组,而不需要访问 hits 对象。所以例如var suggestion = response[0]; address_input.value = suggestion.country;
猜你喜欢
  • 2018-10-09
  • 2021-01-26
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多