WGS84(GPS坐标) BD09坐标(百度坐标)GCJ02(国测局坐标、高德) 的相互转换
关于三种坐标系的介绍
- WGS84:一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
- GCJ02:由中国国家测绘局制订的地理信息系统的坐标系统,是由WGS84坐标系经过加密后的坐标系。
- BD09:百度坐标系,在GCJ02坐标系基础上再次加密。其中BD09LL表示百度经纬度坐标,BD09MC表示百度墨卡托米制坐标。
使用手机(如安卓)的默认GPS定位使用的是WGS84坐标系,百度地图使用的自家BD09LL坐标系,高德地图和腾讯地图都是GCJ02即火星坐标系,所以相互之间是需要转换的,不然会有位置偏移。
在安卓开发中,使用安卓的location接口获取的经纬度就是WGS84坐标系。而高德地图和腾讯地图使用的是GCJ02坐标系,百度地图使用的是BD09坐标系。
BD09 和 GCJ02 相互转换
/*** BD-09 坐标转换成 GCJ-02 坐标*/public static LatLng BD2GCJ(LatLng bd) {double x = bd.longitude - 0.0065, y = bd.latitude - 0.006;double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);double lng = z * Math.cos(theta);double lat = z * Math.sin(theta);return new LatLng(lat, lng);}/*** GCJ-02 坐标转换成 BD-09 坐标*/public static LatLng GCJ2BD(LatLng bd) {double x = bd.longitude, y = bd.latitude;double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * Math.PI);double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math.PI);double tempLon = z * Math.cos(theta) + 0.0065;double tempLat = z * Math.sin(theta) + 0.006;return new LatLng(tempLat, tempLon);}
WGS84(GPS) 转换成 GCJ02(国测局)
Java版本
public class GPSToGCJ {static double pi = 3.14159265358979324; static double a = 6378245.0; static double ee = 0.00669342162296594323; // World Geodetic System ==> Mars Geodetic System //主要调用函数public static double[] transformWGS84ToGCJ(double wgLon,double wgLat) { double[] d=new double[2];double mgLat;double mgLon;if (outOfChina(wgLat, wgLon)){ mgLat = wgLat; mgLon = wgLon; } 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 – 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); mgLat = wgLat + dLat; mgLon = wgLon + dLon; d[0]=mgLon;d[1]=mgLat;return d;} //中国之外则不需要转换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; }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; } 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; }
JS版本
WGS84ToGCJ(wgLon,wgLat){var pi = 3.14159265358979324var ee = 0.00669342162296594323var a = 6378245.0var dLat = this.transformLat(wgLon-105.0, wgLat-35.0)var dLon = this.transformLon(wgLon-105.0, wgLat-35.0)let radLat = wgLat / 180.0 * pi;var magic = Math.sin(radLat);magic = 1 - ee * magic * magic;var sqrtMagic = Math.sqrt(magic);dLat = (dLat * 180.0) / ((a * (1-ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); var mgLat = parseFloat(wgLat) + parseFloat(dLat); var mgLon = parseFloat(wgLon) + parseFloat(dLon); var d = [mgLon,mgLat]return d
},
//转为纬度
transformLat(x,y){var pi = 3.14159265358979324;var 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
},
//转换经度
transformLon(x,y){var pi = 3.14159265358979324;var 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
}
GCJ02 转换成 WGS84(js代码)
gcj02towgs84(lng, lat){// 定义一些常量,坐标转化用const PI = 3.1415926535897932384626;const a = 6378245.0;const ee = 0.00669342162296594323;var dlat = this.transformlat(lng - 105.0, lat - 35.0);var dlng = this.transformlng(lng - 105.0, lat - 35.0);var radlat = (lat / 180.0) * PI;var magic = Math.sin(radlat);magic = 1 - ee * magic * magic;var sqrtmagic = Math.sqrt(magic);dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI);dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);const mglat = parseFloat(lat) + parseFloat(dlat);const mglng = parseFloat(lng) + parseFloat(dlng);console.log("lng*2", lng * 2);console.log(" mglat", mglng);return [lng * 2 - mglng, lat * 2 - mglat];},transformlat(lng, lat){const PI = 3.1415926535897932384626;var ret =-100.0 +2.0 * lng +3.0 * lat +0.2 * lat * lat +0.1 * lng * lat +0.2 * Math.sqrt(Math.abs(lng));ret +=((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *2.0) /3.0;ret +=((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) /3.0;ret +=((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) *2.0) /3.0;console.log("ret", ret);return ret;},transformlng(lng, lat) {const PI = 3.1415926535897932384626;var ret =300.0 +lng +2.0 * lat +0.1 * lng * lng +0.1 * lng * lat +0.1 * Math.sqrt(Math.abs(lng));ret +=((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *2.0) /3.0;ret +=((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) /3.0;ret +=((150.0 * Math.sin((lng / 12.0) * PI) +300.0 * Math.sin((lng / 30.0) * PI)) *2.0) /3.0;return ret;}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
