大疆坐标系和高德地图坐标系之间转换的问题

目前各种坐标系非常多,他们之间的转换就是个比较麻烦的问题。之前有个问题,就是大疆的坐标系在高德地图上显示发生了偏移,但是好在高德地图提供了坐标转换的API,但是新的问题来了,大疆的坐标适应了高德的坐标系,但是选择航点位置是在高德的地图上,执行却是在大疆的坐标系中,于是今天尝试了多种解决方案后,最后发现还是得逆转一下。

大疆坐标系 WGS84 俗称地球坐标系

高德坐标系 GCJ02 俗称火星坐标系(未考)

转换代码如下:

package com.jiataoyuan.toolsapp.utils;

import com.amap.api.maps2d.CoordinateConverter;
import com.amap.api.maps2d.model.LatLng;

public class MapConvertUtils {

    private static double PI = Math.PI; // 圆周率
    private static double AXIS = 6378245.0;  //轴
    private static double OFFSET = 0.00669342162296594323;  //偏移量 (a^2 - b^2) / a^2


    // 坐标转换-转为高德坐标系(高德API)
    public LatLng getGDLatLng(double lat, double lng) {

        CoordinateConverter converter = new CoordinateConverter();
        // CoordType.GPS 待转换坐标类型
        converter.from(CoordinateConverter.CoordType.GPS);
        // sourceLatLng待转换坐标点 DPoint类型
        converter.coord(new LatLng(lat, lng));
        // 执行转换操作
        LatLng desLatLng = converter.convert();
        return new LatLng(desLatLng.latitude, desLatLng.longitude);
    }
    
    
    // WGS84=》GCJ02   地球坐标系=>火星坐标系
    private static double[] wgs2GCJ(double wgLat, double wgLon) {
        double[] latlon = new double[2];
//        if (outOfChina(wgLat, wgLon)) {
//            latlon[0] = wgLat;
//            latlon[1] = wgLon;
//            return latlon;
//        }
        double[] deltaD = delta(wgLat, wgLon);
        latlon[0] = wgLat + deltaD[0];
        latlon[1] = wgLon + deltaD[1];
        return latlon;
    }

    // 坐标转换-转为大疆坐标系
    //GCJ02=>WGS84   火星坐标系=>地球坐标系(精确)
    public static LatLng getDJILatLng(double gcjLat, double gcjLon) {
        double initDelta = 0.01;
        double threshold = 0.000000001;
        double dLat = initDelta, dLon = initDelta;
        double mLat = gcjLat - dLat, mLon = gcjLon - dLon;
        double pLat = gcjLat + dLat, pLon = gcjLon + dLon;
        double wgsLat, wgsLon, i = 0;
        while (true) {
            wgsLat = (mLat + pLat) / 2;
            wgsLon = (mLon + pLon) / 2;
            double[] tmp = wgs2GCJ(wgsLat, wgsLon);
            dLat = tmp[0] - gcjLat;
            dLon = tmp[1] - gcjLon;
            if ((Math.abs(dLat) < threshold) && (Math.abs(dLon) < threshold))
                break;

            if (dLat > 0) pLat = wgsLat;
            else mLat = wgsLat;
            if (dLon > 0) pLon = wgsLon;
            else mLon = wgsLon;

            if (++i > 10000) break;
        }
        return new LatLng(wgsLat, wgsLon);
    }


    // 转换函数
    private static double[] delta(double wgLat, double wgLon) {
        double[] latlng = new double[2];
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - OFFSET * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (AXIS / sqrtMagic * Math.cos(radLat) * PI);
        latlng[0] = dLat;
        latlng[1] = dLon;
        return latlng;
    }

    // 是否超出国界
    private 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;
    }

    // 转换纬度
    private 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;
    }

    // 转换经度
    private 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;
    }
}

你可能感兴趣的:(大疆坐标系和高德地图坐标系之间转换的问题)