繁体   English   中英

使用Algolia places.js库反向地理编码查询

[英]Reverse geocoding query with Algolia places.js library

我正在使用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库进行反向调用的正确方法吗?

谢谢。

所以错误的是Algolia如何提供两种方式来调用反向地理编码,但实际上只记录了一种。

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

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 });

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

暂无
暂无

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

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