js:获取经纬度与计算距离

        //获取当前的经纬度
        protogenesisDistance();
        function protogenesisDistance() {
            var jsonDistance = JSON.parse(window.android_js_interface.getDistance ());
            var currentLon = jsonDistance.lon;
            var currentLat = jsonDistance.lat;
            // var currentLon = 118.788365;
            // var currentLat = 32.046223;
            document.getElementById('currentLon').value = currentLon;
            document.getElementById('currentLat').value = currentLat;
        }
        function Rad(d){
           return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
        }
        //计算距离,参数分别为第一点的纬度,经度;第二点的纬度,经度
        function GetDistance(lat1,lng1,lat2,lng2){
            var radLat1 = Rad(lat1);
            var radLat2 = Rad(lat2);
            var a = radLat1 - radLat2;
            var  b = Rad(lng1) - Rad(lng2);
            var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
            Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
            s = s *6378.137 ;// EARTH_RADIUS;
            s = Math.round(s * 10000) / 10000; //输出为公里
            //s=s.toFixed(4);
            return s;
        }

你可能感兴趣的:(js:获取经纬度与计算距离)