我在網路上找到別人寫好,疑似是C#的版本:
http://www.dotblogs.com.tw/jeff-yeh/archive/2009/02/04/7034.aspx
我改成C/C++的版本:
#define M_PI 3.14159265358979323846
double ConvertDegreeToRadians(double degrees)
{
return (M_PI/180)*degrees;
}
double GetDistance(double Lat1, double Long1, double Lat2, double Long2)
{
double Lat1r = ConvertDegreeToRadians(Lat1);
double Lat2r = ConvertDegreeToRadians(Lat2);
double Long1r = ConvertDegreeToRadians(Long1);
double Long2r = ConvertDegreeToRadians(Long2);
double R = 6371; // Earth's radius (km)
double d = acos(sin(Lat1r) *
sin(Lat2r) + cos(Lat1r) *
cos(Lat2r) *
cos(Long2r-Long1r)) * R;
return d;
}
想要使用這個function很簡單
例如想要得知24.797254,120.969189到24.803545,120.979038的距離,就這樣呼叫:
cout<<GetDistance(24.797254,120.969189,24.803545,120.979038)<<endl;
function回傳的單位是公里。