distance.js 543 B

123456789101112131415
  1. function distance(lat1, lon1, lat2, lon2) {
  2. const R = 6371; // 지구 반지름 (단위: km)
  3. const dLat = deg2rad(lat2 - lat1);
  4. const dLon = deg2rad(lon2 - lon1);
  5. const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
  6. Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
  7. Math.sin(dLon/2) * Math.sin(dLon/2);
  8. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  9. const distance = R * c; // 두 지점 간의 거리 (단위: km)
  10. return distance;
  11. }
  12. function deg2rad(deg) {
  13. return deg * (Math.PI/180);
  14. }