JS中计算两点经纬度距离(代码)
程序员文章站
2022-05-21 14:12:31
js中计算两点经纬度距离(代码)
getdistance:function(lat1, lng1, lat2, lng2, len_type = 2, decimal =...
js中计算两点经纬度距离(代码)
getdistance:function(lat1, lng1, lat2, lng2, len_type = 2, decimal = 2) { var radlat1 = lat1 * math.pi / 180.0; //pi()圆周率 var radlat2 = lat2 * math.pi / 180.0; var a = radlat1 - radlat2; var b = (lng1 * math.pi / 180.0) - (lng2 * math.pi / 180.0); 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; s = math.round(s * 1000); if (len_type --> 1) { s /= 1000; } return math.round(s, decimal); },