大疆坐标系和高德地图坐标系之间转换的问题
生活随笔
收集整理的這篇文章主要介紹了
大疆坐标系和高德地图坐标系之间转换的问题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目前各種坐標(biāo)系非常多,他們之間的轉(zhuǎn)換就是個(gè)比較麻煩的問(wèn)題。之前有個(gè)問(wèn)題,就是大疆的坐標(biāo)系在高德地圖上顯示發(fā)生了偏移,但是好在高德地圖提供了坐標(biāo)轉(zhuǎn)換的API,但是新的問(wèn)題來(lái)了,大疆的坐標(biāo)適應(yīng)了高德的坐標(biāo)系,但是選擇航點(diǎn)位置是在高德的地圖上,執(zhí)行卻是在大疆的坐標(biāo)系中,于是今天嘗試了多種解決方案后,最后發(fā)現(xiàn)還是得逆轉(zhuǎn)一下。
大疆坐標(biāo)系 WGS84 俗稱地球坐標(biāo)系
高德坐標(biāo)系 GCJ02 俗稱火星坐標(biāo)系(未考)
轉(zhuǎn)換代碼如下:
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// 坐標(biāo)轉(zhuǎn)換-轉(zhuǎn)為高德坐標(biāo)系(高德API)public LatLng getGDLatLng(double lat, double lng) {CoordinateConverter converter = new CoordinateConverter();// CoordType.GPS 待轉(zhuǎn)換坐標(biāo)類型converter.from(CoordinateConverter.CoordType.GPS);// sourceLatLng待轉(zhuǎn)換坐標(biāo)點(diǎn) DPoint類型converter.coord(new LatLng(lat, lng));// 執(zhí)行轉(zhuǎn)換操作LatLng desLatLng = converter.convert();return new LatLng(desLatLng.latitude, desLatLng.longitude);}// WGS84=》GCJ02 地球坐標(biāo)系=>火星坐標(biāo)系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;}// 坐標(biāo)轉(zhuǎn)換-轉(zhuǎn)為大疆坐標(biāo)系//GCJ02=>WGS84 火星坐標(biāo)系=>地球坐標(biāo)系(精確)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);}// 轉(zhuǎn)換函數(shù)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;}// 是否超出國(guó)界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;}// 轉(zhuǎn)換緯度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;}// 轉(zhuǎn)換經(jīng)度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;} }總結(jié)
以上是生活随笔為你收集整理的大疆坐标系和高德地图坐标系之间转换的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 珍大户 认知世界的经济学课程推荐电影《大
- 下一篇: 辽东楤木的高质量参考基因组组装和遗传转化