Home > Web Front-end > JS Tutorial > JS longitude and latitude distance calculation code sharing

JS longitude and latitude distance calculation code sharing

小云云
Release: 2018-03-06 15:07:15
Original
2115 people have browsed it

This article mainly shares with you the JS longitude and latitude distance calculation code. I hope you can use the following code to learn to calculate the JS longitude and latitude distance. I hope it can help you.

 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);  
},
Copy after login

Related recommendations:

php Tencent map longitude and latitude conversion to Baidu map longitude and latitude implementation ideas and example sharing

javascript - WeChat js acquisition How to transmit values ​​of longitude and latitude and how to receive them in the background

Calculate the distance between the longitude and latitude of two points under SQL SERVER

The above is the detailed content of JS longitude and latitude distance calculation code sharing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template