我有一个网站,其中包含一个包含位置列表的表单。我希望默认选择是最接近用户当前位置的位置。
我知道如何使用 Javascript 获取当前 GPS 位置,但问题是获取位置需要一些时间,我希望立即显示表单,并在(或如果)位置已恢复。
我以前从未使用过 Jquery - 谁能给我一些入门帮助?
位置存储在 MySQL 数据库中,我使用 PHP 检索数据。
最佳答案
如果你想实现这一点,你应该遵循以下步骤:
- 将选择中包含的所有位置存储到数据库中 与他们的坐标。
- 将用户位置坐标存储到 session 中
- 从数据库中选择所有位置到一个数组中并重新排序 使用下面的函数( session 中的用户坐标和数据库中的每个位置坐标)
- 显示从数组中选择的新结果
最初发布 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
};
关于php - 根据到当前 GPS 位置的距离重新排序 <select> 位置列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215756/