【问题标题】:Radius of longitude and latitude in HTML5 geolocationHTML5地理定位中的经纬度半径
【发布时间】:2013-03-01 23:02:21
【问题描述】:

所以我环顾四周,找不到任何真正回答我想要做的事情的东西,因此我发布了!

我的总体目标本质上是让页面读取用户的位置,然后根据他们所在的位置运行代码。具体来说,我有一个 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!');};
    } 

【问题讨论】:

  • 顺便说一下,代码中倒数第二行将 lat 和 long 都设置为 0,我怀疑这就是你的意思:if (lat = 0 && long = 0) {alert('It works!');}; 可能应该是 if (lat == 0 && long == 0) {alert('It works!');};
  • 好收获!我没看到这个。我已经将它从实际坐标编辑为 0,所以如果人们认为我指向 0/0 很奇怪,我不是。 :p

标签: javascript geolocation


【解决方案1】:

我要做的是使用 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
  }
}

【讨论】:

  • 这个数字代表“6370981.162”是什么?
  • 将地球的 acos 转换为米。它本质上是行星的平均半径,以米为单位。如果您要使用另一个星球进行这些转换,您将使用不同的常数。
  • 我需要将 CalculateDistance 返回的距离除以 100 以获得实际米数:S
  • 只是想知道,为什么 safari iphone 返回显着不同的距离值(iOS 15.3.1 - iphone 11)。在我的情况下,它返回 413618.71376653085 而在 android 或 chrome 上和 mac 上的 safarin 返回 1005.8428294
  • @webchun 我看不到您的示例,但您可能不允许 iPhone 上的 Safari 读取您的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2014-01-18
相关资源
最近更新 更多