【问题标题】:How do I convert coordinates into an address in JS?如何在JS中将坐标转换为地址?
【发布时间】: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


    【解决方案1】:

    您可以使用googlereverse geocoding从坐标中获取地址详细信息。

    获取坐标后,使用geocoding 从中获取位置。你可以从here看到样本

    【讨论】:

    • 感谢您的链接。虽然我对 JS 很糟糕,但无法弄清楚如何使示例中的代码与我的代码一起使用。
    • 你是如何获取坐标的?
    • 您可以参考this & this SO 答案和this 资源,这将解决您的问题。如果有任何问题,请随时询问。
    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    相关资源
    最近更新 更多