根据到当前GPS位置的距离</select>重新排序<select>位置列表

时间:2014-04-22 09:36:05

标签: php jquery mysql gps html-lists

我的网站上有一个包含地点列表的表单。我希望默认选择是最接近用户当前位置的位置。

我知道如何使用Javascript获取当前的GPS位置,但问题是获取位置需要一些时间,我希望立即显示该表单,并且只需重新排序/选择最接近的项目时(或如果)地点被重新开始。

我之前从未使用过Jquery - 有人可以给我一些帮助吗?

这些位置存储在MySQL数据库中,我使用PHP来检索数据。

1 个答案:

答案 0 :(得分:1)

如果您想实现这一目标,请按以下步骤操作:

  • 将select中包含的所有位置存储到数据库中 与他们的coords。
  • 将用户位置记录存储到会话中
  • 从数据库中选择所有位置到一个数组并重新排序 使用下面的功能(会话中的用户坐标和数据库中的每个位置坐标)
  • 显示数组结果中的新选择

原始发布here

var rad = function(x) {
  return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
  var R = 6378137; // Earth’s mean radius in meter
  var dLat = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d; // returns the distance in meter
};