经纬度工具类GPSUtil

package com.new3s.manage.util;


import org.apache.commons.lang3.StringUtils;

public class GPSUtil {

    public static String gpsToDecimals(String gpsParam) {
        String direction = gpsParam.substring(0, 1);
        String gps = gpsParam.substring(1);
        String[] gpsSplit = gps.split("°");
        Double gpsLimit = Double.valueOf(gpsSplit[0]);
        Double gpsMinute = Double.valueOf(gpsSplit[1].split("′")[0]);
        Double gpsSecond = Double.valueOf(gpsSplit[1].split("′")[1].split("″")[0]);
        double v = gpsLimit + gpsMinute / 60 + gpsSecond / 3600;
        v = (double) Math.round(v * 1000000000) / 1000000000;
        if (StringUtils.equals(direction, "N") || StringUtils.equals(direction, "E")) {
            return String.valueOf(v);
        } else {
            return "-" + v;
        }
    }

    public static String gpsToDecimalsEn(String gpsParam) {
        String direction = gpsParam.substring(0, 1);
        String gps = gpsParam.substring(1);
        String[] gpsSplit = gps.split("°");
        Double gpsLimit = Double.valueOf(gpsSplit[0]);
        Double gpsMinute = Double.valueOf(gpsSplit[1].split("\'")[0]);
        Double gpsSecond = Double.valueOf(gpsSplit[1].split("\'")[1].split("\"")[0]);
        double v = gpsLimit + gpsMinute / 60 + gpsSecond / 3600;
        v = (double) Math.round(v * 1000000000) / 1000000000;
        if (StringUtils.equals(direction, "N") || StringUtils.equals(direction, "E")) {
            return String.valueOf(v);
        } else {
            return "-" + v;
        }
    }

    /**
     * @param gpsParam
     * @param type     1:经度  2:纬度
     * @return
     */
    public static String decimalsToGps(String gpsParam, int type) {
        Double gps = new Double(gpsParam);
        String direction = "";
        if (gps > 0) {
            if (type == 1) {
                direction = "E";
            } else {
                direction = "N";
            }
        } else {
            if (type == 1) {
                direction = "W";
            } else {
                direction = "S";
            }
        }
        String[] gpsSplit = gpsParam.split("\\.");
        //取整为度数
        String gpsLimit = gpsSplit[0];
        double v = Double.valueOf("0." + gpsSplit[1]) * 60;
        //取小数位 * 60 再取整为 分数
        int gpsMinute = (int) Math.floor(v);
        //取分数的小数位 * 60 为秒数,保留两位小数
        double gpsSecond = (v - gpsMinute) * 60;
        return direction + gpsLimit + "°" + gpsMinute + "′" + String.format("%.2f", gpsSecond) + "″";
    }

    /**
     * gps格式验证
     * @param gps
     * @return
     */
    public static boolean gpsFormatCheck(String gps) {
        if (gps == null || StringUtils.equals("", gps)) {
            return false;
        }
        if (gps.contains("°") && gps.contains("′") && gps.contains("″")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
//        if (aDouble)
//        String str = "N107°34'54.08\"";
        String str = "N38°30′47.24″";

        boolean check = GPSUtil.gpsFormatCheck(str);
        System.out.println(check);

        System.out.println(GPSUtil.decimalsToGps("116.75659799", 1));


/*
        String aDouble = GPSUtil.gpsToDecimalsEn(str);
        System.out.println("N107°34'54.08\"");
        System.out.println(aDouble);
        String decimalsToGps = GPSUtil.decimalsToGps(aDouble, 1);
        System.out.println(decimalsToGps);*/
    }
}

 

posted @ 2023-01-14 15:41  xiexie0812  阅读(47)  评论(0编辑  收藏  举报