■ 거리를 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
using System; #region 거리 구하기 - GetDistance(latitude1, longitude1, latitude2, longitud2, unit) /// <summary> /// 거리 구하기 /// </summary> /// <param name="latitude1">위도 1</param> /// <param name="longitude1">경도 1</param> /// <param name="latitude2">위도 2</param> /// <param name="longitud2">경고 2</param> /// <param name="unit">단위</param> /// <returns>거리</returns> public double GetDistance(double latitude1, double longitude1, double latitude2, double longitud2, char unit) { double theta; double distance; theta = longitude1 - longitud2; distance = Math.Sin(GetRadian(latitude1)) * Math.Sin(GetRadian(latitude2)) + Math.Cos(GetRadian(latitude1)) * Math.Cos(GetRadian(latitude2)) * Math.Cos(GetRadian(theta)); distance = Math.Acos(distance); distance = GetDegree(distance); distance = distance * 60 * 1.1515; switch(unit) { // Statute mile : 법정 마일(약 1,609km) case 'm' : case 'M' : break; // 킬로미터 case 'k' : case 'K' : distance = distance * 1.609344; break; // Nautical mile : 해리(1,852미터) case 'n' : case 'N' : distance = distance * 0.8684; break; } return distance; } #endregion #region 라디안 구하기 - GetRadian(degree) /// <summary> /// 라디안 구하기 /// </summary> /// <param name="degree">도</param> /// <returns>라디안</returns> private double GetRadian(double degree) { return (degree * Math.PI / 180); } #endregion #region 도 구하기 - GetDegree(radian) /// <summary> /// 도 구하기 /// </summary> /// <param name="radian">라디안</param> /// <returns>도</returns> private double GetDegree(double radian) { return (radian * 180 / Math.PI); } #endregion |