JAVA将北斗定位系统坐标系用于高德地图或百度地图

1,743 阅读1分钟

常用坐标系介绍

  • WGS-84(GPS) 国际标准,一般从国际标准的GPS设备获取的坐标都是WGS-84,以及国际地图提供商使用的坐标系。

  • GCJ-02 中国标准,国测局02年发布的坐标系。又称“火星坐标”。在中国,必须至少使用“GCJ-02”对地理位置进行首次加密。比如谷歌中国、高德、腾讯都在用这个坐标系。

  • BD-09 百度标准,在“GCJ-02”的基础上进行二次加密。

img

北斗定位系统坐标

如果有对接过北斗定位系统的小伙伴可能会发现一个问题,北斗坐标经纬度好像都被乘了100?

$GNRMC,083735.000,A,2429.53531,N,11810.78036,E,0.54,171.11,190621,,,A*7E

这个,就是我们北斗定位系统接收到的坐标值,可以发现,北纬是2429.53531,东经是11810.78036,倘若我们直接将这个经纬度除100,北纬24.2953531,东经118.1078036,扔到百度或者高德地图中,我们会发现,坐标偏移的离谱。

高德地图定位:

image-20210628153906487.png

百度地图定位:

image-20210628154438360.png

可以发现,两者的距离都偏移了几十公里,这个原因就在于,国内,无论是高德还是百度,它们所使用的经纬度值,都是经过加密之后的值,而不是直接用GPS的实际经纬度来定位的,因此,我们如果想要将北斗定位系统的坐标系,用于商业地图上,则需要将它经过对应的加密转换处理才能够正确的使用。

坐标系的相互转换

首先,要强调一点,其实我们北斗定位的坐标系值并不是简单的100倍关系,而是需要做一次度分秒的转换的。则我们获取的北斗坐标值,北纬2429.53531,东经11810.78036,需要做如下计算:24+(29.53531/60)≈ 24.49225517 118+(10.78036/60)≈118.17967267

这两个值才是我们应该带入转换公式的坐标值,如果直接用100倍关系的值去转换,定位一样会偏差

接着上代码

  public static final String BAIDU_LBS_TYPE = "bd09ll";
    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    /**
     * 北斗坐标系度分秒计算
     * @param msg
     * @return
     */
    public static GPS str_To_Gps84(String msg){
        double lat = 0;
        double lon = 0;
        String[] split = msg.split(",");
        if (split[4].equals("N") || split[4].equals("S")){
            int i = split[3].indexOf(".");
            String latInt = split[3].substring(0, i - 2);
            String latDec = split[3].substring(i-2);
            int i1 = split[5].indexOf(".");
            String lonInt = split[5].substring(0, i1-2);
            String lonDec = split[5].substring(i1-2);
            lat = Integer.parseInt(latInt) + Double.parseDouble(latDec)/60;
            lon = Integer.parseInt(lonInt) + Double.parseDouble(lonDec)/60;
            System.out.println("latInt = "+ latInt);
            System.out.println("latDec = "+ latDec);
            System.out.println("lonInt = "+ lonInt);
            System.out.println("lonDec = "+ lonDec);
            System.out.println("lat = "+ lat);
            System.out.println("lon = "+ lon);
        } else {
            return null;
        }

        return new GPS(lat,lon);
    }




    /**
     * 84 to 火星坐标系 (GCJ-02)
     * @param lat
     * @param lon
     */
    public static GPS gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon))
        {
            return null;
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    /**
     * * 火星坐标系 (GCJ-02) to 84
     * * @param lon * @param lat * @return
     */
    public static GPS gcj_To_Gps84(double lat, double lon) {
        GPS gps = transform(lat, lon);
        double lontitude = lon * 2 - gps.getLon();
        double latitude = lat * 2 - gps.getLat();
        return new GPS(latitude, lontitude);
    }

    /**
     * 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param gg_lat
     * @param gg_lon
     */
    public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
        double bd_lon = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new GPS(bd_lat, bd_lon);
    }

    /**
     * * 将 BD-09 坐标转换成GCJ-02 坐标 *
     * * @param
     * bd_lat * @param bd_lon * @return
     */
    public static GPS bd09_To_Gcj02(double bd_lat, double bd_lon) {
        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new GPS(gg_lat, gg_lon);
    }

    /**
     * (BD-09)-->84
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static GPS bd09_To_Gps84(double bd_lat, double bd_lon) {

        GPS gcj02 = bd09_To_Gcj02(bd_lat, bd_lon);
        GPS map84 = gcj_To_Gps84(gcj02.getLat(),
                gcj02.getLon());
        return map84;

    }

    /**
     * is or not outOfChina
     * @param lat
     * @param lon
     * @return
     */
    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    public static GPS transform(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new GPS(lat, lon);
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new GPS(mgLat, mgLon);
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }


    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }
//求两坐标距离
    public static double distance(double lonA,double latA,double lonB,double latB){
        int earthR = 6371000;
        double x = Math.cos(latA * PI / 180.) * Math.cos(latB * PI / 180.) * Math.cos((lonA - lonB) * PI / 180);
        double y = Math.sin(latA * PI / 180.) * Math.sin(latB * PI / 180.);
        double s = x + y;
        if (s > 1) s = 1;
        if (s < -1) s = -1;
        double alpha = Math.acos(s);
        double distance = alpha * earthR;
        return distance;
    }
    //重载,根据两个点求距离
    public static double distance(GPS p1,GPS p2){
        return distance(p1.getLon(), p1.getLat(), p2.getLon(), p2.getLat());
    }

高德地图用的GCJ-02坐标系转换结果:

public CommonResult test() {
        String msg = "$GNRMC,083735.000,A,2429.53531,N,11810.78036,E,0.54,171.11,190621,,,A*7E";

        GPS gps2 = GPSConverterUtils.str_To_Gps84(msg);

        GPS gps = GPSConverterUtils.gps84_To_Gcj02(gps2.getLat(), gps2.getLon());
        return CommonResult.success(gps);
    }
 "lat": 24.489503175446604,    "lon": 118.18449589009587

image-20210628155701852.png

可以看到转换过来后的坐标系,精确定位到了我们办公楼

百度坐标系的BD-09坐标系转换结果:

    
public CommonResult test() {
        String msg = "$GNRMC,083735.000,A,2429.53531,N,11810.78036,E,0.54,171.11,190621,,,A*7E";
    GPS gps2 = GPSConverterUtils.str_To_Gps84(msg);

    GPS gps = GPSConverterUtils.gps84_To_Gcj02(gps2.getLat(), gps2.getLon());
     GPS gps1 = GPSConverterUtils.gcj02_To_Bd09(gps.getLat(), gps.getLon());
    return CommonResult.success(gps1);
}
  "lat": 24.49580387761381,    "lon": 118.19095399371493

image-20210628160007620.png

可以看到百度地图也精确的定位到了我们的办公楼

结论

 谷歌地图那些,用的是WGS-84坐标系,北斗定位系统给到的坐标值要做一次度分秒的转换,转换后才是84坐标,像高德地图那些用的是GCJ-02火星坐标系,等于要先做一次GCJ-02加密,坐标系才能在高德地图用,百度地图则需要再做一次BD-09的加密