首页 > 解决方案 > 将纬度和经度值转换为地名并显示?

问题描述

有没有办法将纬度和经度值转换为地名,然后能够在 HTML 页面上显示该名称?

if ('geolocation' in navigator) {
  let watchID = navigator.geolocation.watchPosition(position => {
    let latitude = position.coords.latitude;
    let longitude = position.coords.longitude;
    console.log(latitude, longitude);
  }, error => {
    console.log(error.code);
  }, {
    enableHighAccuracy: true
  });
  console.log(watchID);
} else {
  console.log("Not Supported");
}

代码可以返回经纬度并显示出来,如果可能的话,我想获取具体的地方,然后显示在页面上还是记录在表格中?

标签: javascriptjquerygeolocation

解决方案


基本上,你需要三样东西:

  1. 带有坐标的地点列表。这有点超出了本网站的范围。

  2. 距离函数。您可以为此使用余弦规则或半正弦规则。

  3. 比 O(n) 更好的最近邻算法。为此,您可以例如将您的集合从 1 分区,为此,谷歌“球体三角形镶嵌”,然后您可以优化您的最近邻算法。


推荐阅读