使用 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 });
请注意必须如何将 latitude/longitude 对作为第一个参数给出,然后将查询参数作为第二个对象参数。
我正在使用 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 });
请注意必须如何将 latitude/longitude 对作为第一个参数给出,然后将查询参数作为第二个对象参数。