package org.springblade.common.utils; import java.math.BigDecimal; public class PositionUtil { /** * 度分秒转经纬度 * @param lng * @return */ public static Double tranformPos(String lng){ String[] lntArr = lng .trim() .replace("°", ";") .replace("˚", ";") .replace("′", ";") .replace("'", ";") .replace("″", ";") .replace("\"", "") .split(";"); double degrees = Double.parseDouble(lntArr[0]); double minutes = Double.parseDouble(lntArr[1])/60; double seconds = Double.parseDouble(lntArr[2])/3600; double decimal = degrees + minutes + seconds; BigDecimal bd = new BigDecimal(decimal); double roundedNumber = bd.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue(); // 返回 return roundedNumber; } /** * 度分秒转经纬度 * * @param dms 116°25'7.85" * @return 116.418847 */ public static Double dfm2LatLng(String dms) { Double flag = 0.0; if (dms == null) return flag; try { dms = dms.replace(" ", ""); String[] str2 = dms.split("°"); if (str2.length < 2) return flag; int d = Integer.parseInt(str2[0]); String[] str3 = str2[1].split("\'"); if (str3.length < 2) return flag; int f = Integer.parseInt(str3[0]); String str4 = str3[1].substring(0, str3[1].length() - 1); double m = Double.parseDouble(str4); double fen = f + (m / 60); double du = (fen / 60) + Math.abs(d); if (d < 0) du = -du; return Double.parseDouble(String.format("%.7f", du)); } catch (Exception e) { e.printStackTrace(); } return flag; } // 引用形式的描述信息 public static Double DMSToDecimal(String dms) { String[] parts = dms.split("[°'\"\\s]"); double degrees = Double.parseDouble(parts[0]); double minutes = Double.parseDouble(parts[1]); double seconds = Double.parseDouble(parts[2]); double decimal = degrees + minutes/60 + seconds/3600; return decimal; } public static void main(String[] args) { String lng = "27°16′48″"; Double aDouble = tranformPos(lng); System.out.println("aDouble = " + aDouble); } }