首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个gps坐标之间的距离- javascript

两个gps坐标之间的距离- javascript
EN

Stack Overflow用户
提问于 2015-05-29 18:43:20
回答 2查看 3.7K关注 0票数 0

从上个月开始,我一直在寻找这背后的概念,直到现在我才得到了这些资源。

  1. 如何计算两个经纬度点之间的距离?
  2. geolib.js
  3. 计算两个坐标之间距离的函数显示错误。
  4. 计算纬度/经度点之间的距离、方位及更多

,但我仍然无法确定指定坐标之间的正确距离

作为一个例子,我有两个坐标

启动lat: 26.26594开始长: 78.2095508目标lat: 21.24386目的地长: 81.611

我得到的结果是655.358公里,但实际上(我用我的自行车的里程计测量的)这些距离只有3公里,然后我得到了这个答案。

我甚至检查了我在那个geolib.js上的实现(我在列表项中指定了第二个项),它也显示了相同的结果(655.358)。

我搞不懂

JS

代码语言:javascript
复制
// initialising 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函数(下面是下面的代码)

代码语言:javascript
复制
restLongitude = response['longitude'];
restLatitude = response['latitude'];
geoLocationInit(); /* getting location initialisation */
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-29 20:01:24

经过进一步的研究,在帕特里克·埃文斯的帮助下,我发现,由于台式机上缺少gps设备,我遇到了这个问题,因为坐标不够精确,导致了对距离的错误计算。

根据火狐-位置感知浏览

不同位置的精度差别很大。在一些地方,我们的服务提供者可能能够提供一个地点在几米以内。然而,在其他领域,情况可能远不止如此。所有由我们的服务供应商返回的地点只是估计,我们不保证提供的地点的准确性。请不要在紧急情况下使用此信息。一定要用常识。

位置感知浏览-地点有多精确?

因此,如果想要找到几乎准确的gps坐标,那么应该使用GPS设备或HTML5地理定位API移动设备中,这通常是与GPS硬件一起安装的,因此可以得到精确的坐标。

票数 1
EN

Stack Overflow用户

发布于 2015-05-29 20:14:20

我试图修改该方法的选项:“geoLocationInit”来尝试访问最佳位置,但如果是智能手机,则应该使用wifi和gps。

会是这样的:

代码语言:javascript
复制
var options = {timeout:120000, enableHighAccuracy: true};
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30536869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档