vue3实现高德坐标和gps坐标互转:
/** * 高德地图坐标转GPS坐标算法 */ const pi = 3.1415926535897932384626; const a = 6378245.0; //长半轴 const ee = 0.00669342162296594323; //扁率//地球坐标系(WGS-84)转火星坐标系(GCJ) export function WgsToGcj02(wglng, wglat){ if (outOfChina(wglng, wglat)) { var wglnglat={lng:wglng,lat:wglat}; return wglnglat; } let dlat = transformLat(wglng - 105.0, wglat - 35.0); let dlng = transformLng(wglng - 105.0, wglat - 35.0); let radlat = wglat / 180.0 * pi; let magic = Math.sin(radlat); magic = 1 - ee * magic * magic; let sqrtmagic = Math.sqrt(magic); dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi); dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
var lng = wglng + dlng; var lat = wglat + dlat; var mars_point={lng:lng,lat:lat}; return mars_point; } //火星坐标系(GCJ)转地球坐标系(WGS-84) export function Gcj02ToWgs(wglng, wglat){ if (outOfChina(wglng, wglat)) { var wglnglat={lng:wglng,lat:wglat}; return wglnglat; } let dlat = transformLat(wglng - 105.0, wglat - 35.0); let dlng = transformLng(wglng - 105.0, wglat - 35.0); let radlat = wglat / 180.0 * pi; let magic = Math.sin(radlat); magic = 1 - ee * magic * magic; let sqrtmagic = Math.sqrt(magic); dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi); dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi); let mglat = wglat + dlat; let mglng = wglng + dlng;
var lng = wglng * 2 - mglng; var lat = wglat * 2 - mglat; var mars_point={lng:lng,lat:lat}; return mars_point; } //判断是否在国内 export function outOfChina(wglng, wglat){ if ((wglng < 73.66|| wglng > 135.05) && (wglat < 3.86 || wglat > 53.55)) { return true; } else { return false; } } //纬度转换 export function transformLat(x,y){ let 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; } //经度转换 export function transformLng(x,y){ let 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; } 标签:wglat,sin,wglng,let,坐标,pi,高德,Math,gps From: https://www.cnblogs.com/fenxiangboke/p/18280181