【发布时间】:2019-09-29 08:59:21
【问题描述】:
我使用地理定位 API 返回用户的坐标。我不想返回纬度和经度,而是想返回这些坐标的地址。有什么办法可以做到这一点吗?我将 Rails 用于后端,因此考虑使用地理编码器 gem,但使用 JS 执行此操作并将所有地理编码逻辑保存在一个地方可能更有意义。我目前有以下代码。
var currentLocation = document.getElementById('coordinatesStore');
document.querySelector('.add-button').addEventListener('click', () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(({ coords: { latitude, longitude }}) => {
currentLocation.value = latitude + ", " + longitude;
});
} else {
currentLocation.value = "Geolocation is not supported by this browser.";
}
});
【问题讨论】:
标签: javascript ruby-on-rails geocoding rails-geocoder