经度和纬度的半径在HTML5地理位置中的含义

6

我查了一下,没有找到真正回答我所要做的事情的内容,因此我在这里发布!

我的总体目标是让页面读取用户位置,然后根据他们的位置运行代码。具体来说,我有一个Facebook签到脚本,如果用户在特定位置,则可以允许用户签到。

问题是所涉及的位置比较大,因此手动输入位置的经纬度不起作用。我现在遇到的问题是是否可能告诉JS获取位置的硬编码经度和纬度,但是给出坐标周围的半径(假设为200米),因此当用户进入坐标的200m半径时,代码将被激活。

有人有什么想法吗?

这是我目前的代码。

    jQuery(window).ready(function(){   
        initiate_geolocation();
    });  
    function initiate_geolocation() {  
        navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  
    }  
    function handle_errors(error)  
    {  
        switch(error.code)  
        {  
            case error.PERMISSION_DENIED: alert("user did not share geolocation data");  
            break;  
            case error.POSITION_UNAVAILABLE: alert("could not detect current position");  
            break;  
            case error.TIMEOUT: alert("retrieving position timed out");  
            break;  
            default: alert("unknown error");  
            break;  
        }  
    }  
     function handle_geolocation_query(position){  
         var lat = position.coords.latitude;
         var long = position.coords.longitude;

                      //these are for testing purposes
          alert('Your latitude is '+lat+' and longitude is '+long);
          if (lat == 0 && long == 0) {alert('It works!');};
    } 

1
顺便提一下,你代码中倒数第二行将lat和long都设置为0,我怀疑这不是你想要做的:if (lat = 0 && long = 0) {alert('It works!');};应该改为if (lat == 0 && long == 0) {alert('It works!');}; - Mark Ormston
好的发现!我没有看到这个。我已经将它从实际坐标编辑为0,以防万一有人认为我指向0/0很奇怪,其实不是。:p - Purify
1个回答

14
我会创建一个带有setInterval的投票函数,每1到10秒执行一次,具体取决于您的测试需要,然后只需测试距离即可。这是一个测试两个经纬度之间距离的函数:
function CalculateDistance(lat1, long1, lat2, long2) {
    // Translate to a distance
    var distance =
      Math.sin(lat1 * Math.PI) * Math.sin(lat2 * Math.PI) +
      Math.cos(lat1 * Math.PI) * Math.cos(lat2 * Math.PI) * Math.cos(Math.abs(long1 - long2) * Math.PI);

    // Return the distance in miles
    //return Math.acos(distance) * 3958.754;

    // Return the distance in meters
    return Math.acos(distance) * 6370981.162;
} // CalculateDistance

您的间隔函数应该看起来像这样:

// The target longitude and latitude
var targetlong = 23.456;
var targetlat = 21.098;

// Start an interval every 1s
var OurInterval = setInterval(OnInterval, 1000);

// Call this on an interval
function OnInterval() {
  // Get the coordinates they are at
  var lat = position.coords.latitude;
  var long = position.coords.longitude;
  var distance = CalculateDistance(targetlat, targetlong, lat, long);

  // Is it in the right distance? (200m)
  if (distance <= 200) {
    // Stop the interval
    stopInterval(OurInterval);

    // Do something here cause they reached their destination
  }
}

1
这个数字“6370981.162”代表什么? - Uzair Ali
2
这是将acos转换为地球米的过程。本质上,它是以米为单位的行星平均半径。如果您要使用另一个行星进行转换,则需要使用不同的常数。 - Mark Ormston
我需要将从CalculateDistance返回的距离除以100,以获得真实的米数:S - J.J.
只是好奇,为什么Safari iPhone返回的距离值(iOS 15.3.1 - iPhone 11)会有显著不同。在我的情况下,它返回了413618.71376653085,而在Android或Chrome以及Mac上的Safari返回了1005.8428294。 - webchun
@Webchun,我看不到你的示例,但可能是因为你没有允许Safari在iPhone上读取你的位置。 - Mark Ormston

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