从我的位置开始,地理定位最近的位置(纬度、经度)是什么?

27

我希望根据我的位置显示特定的信息。

我有五个城市,每个城市都有不同的信息,我想显示我最接近哪个城市(以及该城市的信息)。

怎样用JavaScript实现最简单的方式呢?

例如:

如果我将这些城市的纬度和经度存储在一个数组中。

var cities = [
  ['new york', '111111', '222222', 'blablabla']
  ['boston', '111111', '222222', 'blablabla']
  ['seattle', '111111', '222222', 'blablabla']
  ['london', '111111', '222222', 'blablabla']
]

根据我的当前位置(纬度,经度),我想知道离我最近的城市。


3
还没有尝试过任何事情,只是想确认是否有可能。 - Anders Kristoffersson
你可以从https://simplemaps.com/data/world-cities获取城市的经纬度数据。 - Justin
3个回答

57

这里是一个使用HTML5定位获取用户位置的基本代码示例。然后调用NearestCity()并计算位置到每个城市的距离(km)。我选择使用更简单的勾股定理和等距投影来调整经度线的曲率,而不是使用Haversine公式。

// Get User's Coordinate from their Browser
window.onload = function() {
  // HTML5/W3C Geolocation
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(UserLocation);
  }
  // Default to Washington, DC
  else
    NearestCity(38.8951, -77.0367);
}

// Callback function for asynchronous call to HTML5 geolocation
function UserLocation(position) {
  NearestCity(position.coords.latitude, position.coords.longitude);
}


// Convert Degress to Radians
function Deg2Rad(deg) {
  return deg * Math.PI / 180;
}

function PythagorasEquirectangular(lat1, lon1, lat2, lon2) {
  lat1 = Deg2Rad(lat1);
  lat2 = Deg2Rad(lat2);
  lon1 = Deg2Rad(lon1);
  lon2 = Deg2Rad(lon2);
  var R = 6371; // km
  var x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
  var y = (lat2 - lat1);
  var d = Math.sqrt(x * x + y * y) * R;
  return d;
}

var lat = 20; // user's latitude
var lon = 40; // user's longitude

var cities = [
  ["city1", 10, 50, "blah"],
  ["city2", 40, 60, "blah"],
  ["city3", 25, 10, "blah"],
  ["city4", 5, 80, "blah"]
];

function NearestCity(latitude, longitude) {
  var minDif = 99999;
  var closest;

  for (index = 0; index < cities.length; ++index) {
    var dif = PythagorasEquirectangular(latitude, longitude, cities[index][1], cities[index][2]);
    if (dif < minDif) {
      closest = index;
      minDif = dif;
    }
  }

  // echo the nearest city
  alert(cities[closest]);
}

请注意,以下一行存在打字错误: var dif = PythagorasEquirectangular(lat,lon,cities [index] [1],cities [index] [2]); 应为 var dif = PythagorasEquirectangular(latitude,longitude,cities [index] [1],cities [index] [2]); - Kennet
@Kennet - 感谢您让我知道这个错别字。我已经在答案中修正了它。 - Andrew - OpenGeoCode
这太棒了。感谢@Andrew-OpenGeoCode。 - AndrewLeonardi
有人可以详细解释一下这段代码是如何工作的吗? - Akhil S
变量minDif的值为99999;您好,请解释一下它的价值和目的是什么。 - SDK
显示剩余3条评论

21

使用HTML5,您可以获取用户的位置,并使用一个Haversine函数进行比较(下面的函数来自这里):

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接