【问题标题】:Geolocation closest location(lat, long) from my position地理位置离我的位置最近的位置(纬度,经度)
【发布时间】:2014-02-12 07:29:19
【问题描述】:

我想根据我所在的位置显示特定信息。

我有五个信息不同的城市,我想显示我最接近的城市(信息)。

我如何用最简单的方式做到这一点,使用 javascript。

例如

如果我将城市经纬度存储在一个数组中

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

并且以我当前的位置(纬度,经度),我想要离我最近的城市。

【问题讨论】:

  • 什么都没试过,只是看看是否可行。

标签: javascript jquery html geolocation


【解决方案1】:

这是一个使用 HTML5 地理位置获取用户位置的基本代码示例。然后它调用NearestCity() 并计算从该位置到每个城市的距离(公里)。我继续使用 Haversine 公式,而是使用更简单的毕达哥拉斯公式和 equirectangular 投影来调整经度线的曲率。

// 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, urban[ index ][ 1 ], urban[ index ][ 2 ] );应该是 var dif = PythagorasEquirectangular( latitude, longitude, urban[ index ][ 1 ], urban[ index ][ 2 ] );
  • @Kennet - 感谢您让我知道错字。我在答案中修复了它。
  • 这个。是。惊人的。谢谢@Andrew-OpenGeoCode
  • 这很棒。我想修改它以列出可能最近的三个位置。
  • 谁能详细解释一下这段代码是如何工作的?
【解决方案2】:

使用 HTML5,您可以拉取用户的位置,然后使用 Haversine 函数 (function below taken from here) 比较此示例:

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)
}

【讨论】:

    【解决方案3】:

    您可以根据您所在位置和城市位置按纬度计算距离。并找到最短的并绘制。要计算你可以阅读更多http://www.movable-type.co.uk/scripts/latlong.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多