Android中经纬度与度分秒互转

参考网址:https://copyfuture.com/blogs-details/20191018133144854ah2f6ck7jl4641m

/**
* Degree to Degree minute second. 十进制度转度分秒(xxx° ==> xxx°xxx′xxx″)
*/
public static String getDegreeString(double radian) {
    int degree = 0;
    double minute = 0;
    double second = 0;
    try {
        double d = Math.toDegrees(radian);
        degree = (int)d;
        minute = (d - degree) * 60;
        second = (minute - (int)minute) * 60;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return String.format("%1$s ° %2$s ′ %3$.4f ″", degree, (int)minute, second);
}
/**
* 度分秒转经纬度
* @param dms 116°25'7.85"
* @return 116.418847
*/
public static double changeToDu(String dms) {
    if (dms == null) return 0;
    try {
        dms = dms.replace(" ", "");
        String[] str2 = dms.split("°");
        if (str2.length < 2) return 0;
        int d = Integer.parseInt(str2[0]);
        String[] str3 = str2[1].split("\\′");
        if (str3.length < 2) return 0;
        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 du;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
/**
* 将经纬度转换为度分秒格式 
* @param du 116.418847 
* @return 116°25'7.85" 
*/
public static String changeToDFM(double du) {
	int du1 = (int) du;
	double tp = (du - du1) * 60;
	int fen = (int) tp;
	String miao = String.format("%.2f", Math.abs(((tp - fen) * 60)));
	return du1 + "°" + Math.abs(fen) + "'" + miao + "\"";
}

上一篇:linux 查看当前目录剩余空间,已经使用磁盘空间


下一篇:图论之欧拉路问题