2014-02-21 165 views
-1

我有兩個常量 - GPS寬度和經度。我通過HTML5 JS API獲取用戶位置。 getCurrentPosition()方法提供coords.accuracy,它顯示了我的位置準確性。 我需要檢查常數和getCurrentPosition()中gps座標的距離是否小於10米。JavaScript地理位置:檢查距離2個GPS座標的距離

是否有任何庫或jQuery插件或我必須自己寫它?

+1

我發現這些工作代碼:https://github.com/jdjkelly/jQuery-haversine/blob/master/jquery.haversine.0.2.js和https://github.com/janantala/GPS-距離/ blob/master/javascript/distance.js 有什麼更好? – martin

回答

0

下面的代碼是我在Google Maps API v3中使用的代碼。也許你可以做一些類似於你的圖書館。半正式公式考慮到地球是一個球體,這是我計算距離的最佳選擇。谷歌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; 
}, 
+0

它看起來不錯,但你需要互聯網連接。我想離線(移動)應用程序。 – martin

+1

啊這就是這樣,可能想添加到你的問題,因爲這是不明白的。 –