我正在尋找了解自上個月這背後的理念,到現在我得到了這些資源兩個GPS座標之間的距離 - 的JavaScript
- How do I calculate distance between two latitude-longitude points?
- geolib.js
- Function to calculate distance between two coordinates shows wrong
- Calculate distance, bearing and more between Latitude/Longitude points
但我i st生病無法找出正確的距離指定座標之間
舉一個例子,我有兩個座標
開始緯度:26.26594 開始長:78.2095508 目的地緯度:21.24386 目的地長:81.611
我得到的結果爲655.358公里,但實際上(我用我的自行車的里程錶測量過)這些距離只有3公里,那麼爲什麼我得到這個答案。
我甚至檢查過我在geolib.js上的實現(我在列表項中指定了第二項),它也顯示了相同的結果(655.358)。
我無法弄清楚
JS
// inititalising the distance calculation
function geoLocationInit() {
$('.fd-loc-err').html(''); // empty the error shown, if there is any already
$('.fd-dist-rest-user').html('<img src="/images/loader.gif" alt="loading"/>'); // loader
var options = {timeout:120000};
if (navigator.geolocation) {
navigator.geolocation.watchPosition(getLocation, gotError, options);
}
else {
locationError = 'Your Browser does not support geolocation';
showLocationError(locationError);
}
}
//finding the coordinates of user
function getLocation(current) {
var userLat = current.coords.latitude, userLong = current.coords.longitude;
var distance = getDistance(userLat, userLong, restLatitude, restLongitude);
$('.fd-dist-rest-user').html('~' + (distance/1000).toFixed(2) + ' km away'); // fd-dist-rest-user is the <div> tag where i have show the distance calculated
}
function toRad(value) {
return value * Math.PI/180;
}
// calculating distance using Vincenty Formula
function getDistance(lat1, lon1, lat2, lon2) {
var a = 6378137, b = 6356752.314245, f = 1/298.257223563;
var L = toRad(lon2-lon1);
var U1 = Math.atan((1-f) * Math.tan(toRad(lat1)));
var U2 = Math.atan((1-f) * Math.tan(toRad(lat2)));
var sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
var sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
var lambda = L, lambdaP, iterLimit = 100;
do
{
var sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);
var sinSigma = Math.sqrt((cosU2*sinLambda) * (cosU2*sinLambda) + (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
if (sinSigma==0) return 0;
var cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
var sigma = Math.atan2(sinSigma, cosSigma);
var sinAlpha = cosU1 * cosU2 * sinLambda/sinSigma;
var cosSqAlpha = 1 - sinAlpha*sinAlpha;
var cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
if (isNaN(cos2SigmaM)) cos2SigmaM = 0;
var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
lambdaP = lambda;
lambda = L + (1-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
} while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0);
if (iterLimit==0) return NaN
var uSq = cosSqAlpha * (a*a - b*b)/(b*b);
var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
var deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
var s = b*A*(sigma-deltaSigma);
return s;
}
// Error handling for the geolocation
function gotError(error) {
switch(error.code) {
case 1:
locationError = 'You need to give the permission to use your location to calculate the distances between this restaurant and you <button class="btn btn-danger fd-detect-lctn-try-agn">Please try again</button>';
break;
case 2:
locationError = 'TIME OUT - You need to give permission to use your location to show the distance of this restaurant from your current position <button class="btn btn-danger fd-detect-lctn-try-agn">Please try again</button>';
break;
case 3:
locationError = 'It took to long getting your position, <button class="btn btn-danger fd-detect-lctn">Please try again</button>';
break;
default:
locationError = 'An unknown error has occurred, <button class="btn btn-danger fd-detect-lctn">Please try again</button>';
}
showLocationError(locationError);
}
function showLocationError(msg) {
$('.fd-loc-err').html(msg);
$('.fd-dist-rest-user').html('');
}
我正在從我的數據庫,然後調用上的按鈕,點擊geoLocationInit功能的緯度和經度(下面是下面的代碼)
restLongitude = response['longitude'];
restLatitude = response['latitude'];
geoLocationInit(); /* getting location initialisation */
在此先感謝誰可以解決我的萬阿英,蔣達清
你確定你的GPS座標是正確的?將這些座標放入Google地圖等繪圖程序中,可以看出這兩個GPS座標相距很遠,因此您的計算看起來正確 –
是的,我正在使用我的代碼中的函數getLocation中指定的HTML5地理定位API查找這些座標 –
使用凝膠定位API,桌面瀏覽器或gps打開的移動設備?如果沒有GPS設備,地理位置會嘗試使用WiFi /互聯網連接來猜測GPS位置,這非常準確,除非路由器正在使用GPS設備和廣播。 –