內建百度地圖或者高德地圖後擷取的經緯度都是小數的形式
1.将小數轉換成度分秒
public static double getdPoint(double num) {
double d = num;
int fInt = (int) d;
BigDecimal b1 = new BigDecimal(Double.toString(d));
BigDecimal b2 = new BigDecimal(Integer.toString(fInt));
double dPoint = b1.subtract(b2).floatValue();
return dPoint;
}
public static String convertToSexagesimal(double num) {
//擷取整數部分
int du = (int) Math.floor(Math.abs(num));
double temp = getdPoint(Math.abs(num)) * 60;
//擷取整數部分
int fen = (int) Math.floor(temp);
double miao = getdPoint(temp) * 60;
NumberFormat nf = NumberFormat.getNumberInstance();
//設定數值的小數部分允許的最大位數
nf.setMaximumFractionDigits(0);
if (num < 0)
return "-" + du + "°" + fen + "′" + nf.format(miao) + "″";
return du + "°" + fen + "′" + nf.format(miao) + "″";
}
2.保留6位小數,不足補0
BigDecimal b1 = new BigDecimal(locationLatitude);
String s_latitude= b1.setScale(6, BigDecimal.ROUND_HALF_UP).toString();