GPS座標度分秒轉換

之前使用GPS接收器接收到的經緯度格式都是【度度.分分秒秒秒】,但是手機上卻不是這樣,手機上卻是直接發送出GPS的訊號卻是轉換過的地圖格式,這點害我吃了大虧,因此在這裡分享一下度分秒的轉換方式。

經緯度 → 度分秒(假設有個緯度是23.12345)度數 : 整數的部分23 這個就是轉換後的度數(°)
分數 : 小數的部分0.12345 * 60 = 7.407 ,取整數的部分,因此 7(')就是轉換後的分數
秒數 : 分數計算後的小數部分,也就是0.407 * 60 = 24.42,因此24.42(")就是轉換後的秒數

因此23.12345轉換後就是23° 7' 24.42"

度分秒 → 經緯度(假設有個度數是23° 7' 24.42")轉換公式 : x度 y分 z秒 = x + y/60 + z/3600 度
(23) + (7/60) + (24.42/3600) = 23 + 0.11666.... + 0.00678333.... = 23.123443....


下面為用C#寫的簡單轉換公式

經緯度轉度分秒

//經緯度轉度分秒
private string LatLngToGPS(string text)
{
string result = "";
string[] ary = text.Trim().Split('.');
if (ary.Length == 2)
{
double degree, minute, second, temp;
//ex. 21.12345
if (double.TryParse(ary[0], out degree) && double.TryParse(ary[1], out temp))
{
//取小數位 0.12345
temp = temp / System.Math.Pow(10, ary[1].Length);
//分只留下整數位
minute = Math.Floor(temp * 60);
//取分剩下的小數位
double temp1 = (temp * 60) - Math.Floor(temp * 60);
second = temp1 * 60;
result = degree.ToString() + "°" + minute.ToString("00") + "\'" + second.ToString() + "\"";
}
}
return result;
}


度分秒轉經緯度

//度分秒轉經緯度
private string GPSToLatLng(string text)
{
string result = "";
text = text.Trim();
//必須有度分秒才可進行轉換
if (text.IndexOf('°') != -1 && text.IndexOf('\'') != -1 && text.IndexOf('\"') != -1)
{
double degree, minute, second;
//取得度分秒
if (double.TryParse(text.Split('°')[0], out degree) &&
double.TryParse(text.Split('°')[1].Split('\'')[0], out minute) &&
double.TryParse(text.Split('°')[1].Split('\'')[1].Split('\"')[0], out second)
)
{
//x度 y分 z秒 = x + y/60 + z/3600 度
result = (degree + (minute / 60) + (second / 3600)).ToString();
}
}
return result;
}



本文範例 : 
GPS 度分秒轉換.pdf
GPSConvert.zip 2017.06.01將檔案搬移到Github上,請到此處下載範例檔案。

留言

張貼留言

您好,我是 Lawrence,這裡是我的開發筆記的網誌,如果你對我的文章有任何疑問或者有錯誤的話,歡迎留言讓我知道。