我需要使用Java将GPS点从DDD MM.MMM(字符串)转换为十进制(两个值:纬度和经度)。
例如,我有这个字符串:
String a = "N 39° 28.941 W 0° 23.275"
我需要将该字符串转换为这两个值:
double lat= 39.48235
double lon= -0.38792
如何使用Java代码执行此操作?
答案 0 :(得分:2)
解析这六个值的字符串:
......经度相同
快速破解:
public class CoordTest {
private static String coords = "N 39° 28.941 W 0° 23.275";
public static void main(String[] args) {
String[] cArray = coords.split(" ");
String latSign = cArray[0];
String latDegrees = cArray[1].substring(0, cArray[1].length()-1);
String latSubdegrees = cArray[2];
String lonSign = cArray[3];
String lonDegrees = cArray[4].substring(0, cArray[4].length()-1);
String lonSubdegrees = cArray[5];
double lat = getSign(latSign) * (Integer.valueOf(latDegrees) + convertFromDegreesToDecimal(Double.valueOf(latSubdegrees)));
double lon = getSign(lonSign) * (Integer.valueOf(lonDegrees) + convertFromDegreesToDecimal(Double.valueOf(lonSubdegrees)));
}
private static int getSign(String c){
if (c.equals("N") || c.equals("E")){
return 1;
}
return -1;
}
private static double convertFromDegreesToDecimal(Double value){
double result = value/60d;
return result;
}
}