【问题标题】:JavaScript geolocation: check distance from 2 gps coordinatesJavaScript 地理定位:检查距 2 gps 坐标的距离
【发布时间】:2014-02-21 14:47:13
【问题描述】:

我有两个常数 - gps 纬度和经度。我通过 HTML5 JS API 获得用户位置。 getCurrentPosition() 方法提供 coords.accuracy 显示位置的准确性。 我需要检查与 gps 坐标的距离是否为常数,getCurrentPosition() 是否小于 10 米。

是否有任何库或 jQuery 插件,或者我必须自己编写?

【问题讨论】:

标签: javascript html geolocation gps


【解决方案1】:

下面的代码是我在 Google Maps API v3 中使用的代码。也许你可以对你的库做类似的事情。半正弦公式考虑到地球是一个球体,这是我计算距离的最佳选择。 Google API 附带了一个额外的library 来执行此操作。

通常你会找到你的库来计算距离,然后你会想出自己的逻辑。 因此,再次从这个逻辑中获取您需要的内容并尝试您找到的库。

orderMarkersByDistance: function (markers) {
    var flex = this,
        geodesic = [],
        arrClosest = [],
        to, distance, arrDistances,
        map = this.root.gmap3("get");

    // loop through markers and calculate their distances from the center
    for (var i = 0; i < markers.length; i++) {
        to = new google.maps.LatLng(markers[i].lat, markers[i].lng);
        distance = google.maps.geometry.spherical.computeDistanceBetween(map.center, to);
        geodesic.push([i, distance]);
    }

    // sort the distances
    geodesic.sort(function (a, b) { return a[1] - b[1]; });

    // optionally, take the closest distances
    arrDistances = geodesic.slice(0, 3);

    // return closest markers
    $.each(arrDistances, function (idx, item) {
        arrClosest.push(markers[item[0]]);
    });

    return arrClosest;
},

【讨论】:

  • 看起来不错,但您需要连接互联网。我想要离线(移动)应用。
  • 啊就是这样,可能想将其添加到您的问题中,因为我不清楚。
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多