Haversine公式


 1 Number.prototype.toRadians = function() {
 2     return this * Math.PI / 180;
 3 }
 4 
 5 function distance(latitude1, longitude1, latitude2, longitude2) {
 6     //R是地球的半徑,以KM為單位
 7     var R = 6371;
 8 
 9     var deltaLatitude = (latitude2 - latitude1).toRadians();
10     var deltaLongitude = (longitude2 - longitude1).toRadians();
11     latitude1 = latitude1.toRadians(), latitude2 = latitude2.toRadians();
12 
13     var a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) + Math.cos(latitude1) * Math.cos(latitude2) * Math.sin(deltaLongitude / 2) * Math.sin(deltaLongitude / 2);
14     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
15     var d = R * c;
16     return d;
17 }

 

這段代碼可以計算地球上兩個位置之間的距離。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM