【问题标题】:Measuring distance between specific and current position with html5使用html5测量特定位置和当前位置之间的距离
【发布时间】:2011-05-18 15:05:36
【问题描述】:

正如标题所示。知道如何实现吗?

一直在查看http://www.html5archive.com/2010/12/04/geolocation-api-example-distance-tracking/ 并尝试修改该代码,但没有成功。

最初不需要自动刷新。

但基本功能应该是获取用户位置并告诉用户与特定点之间的距离。

【问题讨论】:

    标签: javascript html geolocation gps


    【解决方案1】:

    密钥代码位于this page 上,与您指定的代码相关联。我已将其转换为以下函数:

    /** Extend Number object with method to convert numeric degrees to radians */
    if (typeof Number.prototype.toRadians == 'undefined') {
        Number.prototype.toRadians = function() { return this * Math.PI / 180; };
    }
    
    function getDistance(lat1,lon1,lat2,lon2) {
        var R = 6371; // km
        var dLat = (lat2-lat1).toRadians();
        var dLon = (lon2-lon1).toRadians(); 
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1.toRadians()) * Math.cos(lat2.toRadians()) * 
                Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        return R * c;
    }
    

    您需要将特定的 lat 和 lon 作为前两个参数传递给它,并将当前的 lat 和 lon 作为后两个参数。

    【讨论】:

    • 并从页面底部获取 toRad() 函数,否则它工作正常!
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 2020-04-13
    • 2012-10-05
    • 1970-01-01
    • 2017-05-15
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多