본문 바로가기
개발/HTML, Javascript, CSS

[Javascript] GPS 좌표상 거리 계산 방법

by 똘똘이박사 2023. 12. 1.

 

function calcDistance(locLat, locLng, myLat, myLng){
    const earth_r = 6371; //지구 반지름(km)
    const dLat = deg2Rad(myLat - locLat);
    const dLng = def2Rad(myLng - locLng);
    const a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
              Math.cos(deg2Rad(LocLat)) * Math.cos(deg2Rad(myLat)) *
              Math.sin(dLng/2) * Math.sin(dLng/2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    const distance = earth_r * c;
    return distance;
}

function deg2Rad(deg){
	return deg * (Math.PI/180);
}
반응형