Forráskód Böngészése

fix: getdistanse with gcj-02

MetaPunkGames 2 hónapja
szülő
commit
c1abe100c0
1 módosított fájl, 26 hozzáadás és 0 törlés
  1. 26 0
      nova-werun/pages/home/sport/sport-home/index.js

+ 26 - 0
nova-werun/pages/home/sport/sport-home/index.js

@@ -186,6 +186,32 @@ Page({
         }
 
     },
+    
+    // 经纬度点距离 gcj-02坐标系
+   haversineDistance(location1, location2) {
+    let lat1 = location1.latitude
+    let lon1 = location1.longitude
+    let lat2 = location2.latitude
+    let lon2 = location2.longitude
+
+     function toRad(degree) {
+         return degree * Math.PI / 180;
+     }
+     const R = 6371e3; // 地球半径,单位为米
+     const φ1 = toRad(lat1);
+     const φ2 = toRad(lat2);
+     const Δφ = toRad(lat2 - lat1);
+     const Δλ = toRad(lon2 - lon1);
+ 
+     const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
+               Math.cos(φ1) * Math.cos(φ2) *
+               Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
+     const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+ 
+     const distance = R * c; // 距离,单位为米
+     return distance;
+ }
+ 
     //切换
     switchTab (e) {
         const index = e.currentTarget.dataset.index;