現在正開發的定位模塊用到的定位設置是塞格車圣導航設備,發送指令返回的經緯度需要轉換成十進制,再到GIS系統獲取地理信息描述。以后需要要經常用到這方面的知識,隨筆寫下。
將經緯度轉換成十進制
公式:
Decimal Degrees = Degrees + minutes/60 + seconds/3600
例:57°55'56.6" =57+55/60+56.6/3600=57.9323888888888
如把經緯度 (longitude,latitude) (205.395583333332,57.9323888888888)轉換據成坐標(Degrees,minutes,seconds)(205°23'44.1",57°55'56.6")。
步驟如下:
1、 直接讀取"度":205
2、(205.395583333332-205)*60=23.734999999920 得到"分":23
3、(23.734999999920-23)*60=44.099999995200 得到"秒":44.1
發送定位指令,終端返回的經緯度信息如下:
(ONE072457A3641.2220N11706.2569E000.000240309C0000400)
按照協議解析

獲得信息體的經緯度是主要,其它不要管,直接用String類的substring()方法截掉,獲取的經緯度
3641.2220N11706.2569E http://www.bt285.cn
- package com.tdt.test;
-
- import com.tdt.api.gis.LocationInfo;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class LonlatConversion {
-
-
-
-
-
-
-
- public static String xypase(String dms, String type) {
- if (dms == null || dms.equals("")) {
- return "0.0";
- }
- double result = 0.0D;
- String temp = "";
-
- if (type.equals("E")) {
- String e1 = dms.substring(0, 3);
-
- String e2 = dms.substring(3, dms.length());
-
- result = Double.parseDouble(e1);
- result += (Double.parseDouble(e2) / 60.0D);
- temp = String.valueOf(result);
- if (temp.length() > 9) {
- temp = e1 + temp.substring(temp.indexOf("."), 9);
- }
- } else if (type.equals("N")) {
- String n1 = dms.substring(0, 2);
- String n2 = dms.substring(2, dms.length());
-
- result = Double.parseDouble(n1);
- result += Double.parseDouble(n2) / 60.0D;
- temp = String.valueOf(result);
- if (temp.length() > 8) {
- temp = n1 + temp.substring(temp.indexOf("."), 8);
- }
- }
- return temp;
- }
- public static void main(String[] args) {
- String info="(ONE072457A3641.2220N11706.2569E000.000240309C0000400)";
- info=info.substring(11,info.length()-13);
-
- String N = info.substring(0, info.indexOf("N"));
-
- String E = info.substring(info.indexOf("N")+1,info.indexOf("E"));
-
- double x = Double.parseDouble(CoordConversion.xypase(E,"E"));
- double y = Double.parseDouble(CoordConversion.xypase(N,"N"));
- String result =LocationInfo.getLocationInfo("test", x, y);
- System.out.println(result);
- }
- }
package com.tdt.test;
import com.tdt.api.gis.LocationInfo;
/**
* <p>Title:坐標轉換 </p>
*
* <p>Description:</p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company:</p>
*
* @author sunnylocus
* @version 1.0 [2009-03-24]
*
*/
public class LonlatConversion {
/**
*
* @param dms 坐標
* @param type 坐標類型
* @return String 解析后的經緯度
*/
public static String xypase(String dms, String type) {
if (dms == null || dms.equals("")) {
return "0.0";
}
double result = 0.0D;
String temp = "";
if (type.equals("E")) {//經度
String e1 = dms.substring(0, 3);//截取3位數字,經度共3位,最多180度
//經度是一倫敦為點作南北兩極的線為0度,所有往西和往東各180度
String e2 = dms.substring(3, dms.length());//需要運算的小數
result = Double.parseDouble(e1);
result += (Double.parseDouble(e2) / 60.0D);
temp = String.valueOf(result);
if (temp.length() > 9) {
temp = e1 + temp.substring(temp.indexOf("."), 9);
}
} else if (type.equals("N")) { //緯度,緯度是以赤道為基準,相當于把地球分兩半,兩個半球面上的點和平面夾角0~90度
String n1 = dms.substring(0, 2);//截取2位,緯度共2位,最多90度
String n2 = dms.substring(2, dms.length());
result = Double.parseDouble(n1);
result += Double.parseDouble(n2) / 60.0D;
temp = String.valueOf(result);
if (temp.length() > 8) {
temp = n1 + temp.substring(temp.indexOf("."), 8);
}
}
return temp;
}
public static void main(String[] args) {
String info="(ONE072457A3641.2220N11706.2569E000.000240309C0000400 http://www.guihua.org )";
info=info.substring(11,info.length()-13);
//緯度
String N = info.substring(0, info.indexOf("N"));
//經度
String E = info.substring(info.indexOf("N")+1,info.indexOf("E"));
//請求gis,獲取地理信息描述
double x = Double.parseDouble(CoordConversion.xypase(E,"E"));
double y = Double.parseDouble(CoordConversion.xypase(N,"N"));
String result =LocationInfo.getLocationInfo("test", x, y); //System.out.println("徑度:"+x+","+"緯度:"+y);
System.out.println(result);
}
}
運行結果
在濟南市,位于輕騎路和八澗堡路附近;在環保科技園國際商務中心和濟南市區賢文莊附近。