地图坐标系转换
不同地图服务之间的坐标转换。
坐标系说明
- WGS84: GPS 标准坐标系(国际通用)
- GCJ02: 火星坐标系(中国高德、腾讯地图)
- BD09: 百度坐标系(百度地图)
转换算法
WGS84 转 GCJ02(火星坐标)
function wgs84ToGcj02(lat, lon) {
// 判断是否在国内
if (!isInChina(lat, lon)) {
return { lat, lon };
}
const a = 6378245.0;
const ee = 0.00669342162296594323;
const dLat = transformLat(lat - 35.0, lon - 105.0);
const dLon = transformLon(lon - 105.0, lat - 35.0);
const radLat = lat / 180.0 * Math.PI;
const magic = Math.sin(radLat);
const magic2 = 1 - ee * magic * magic;
const sqrtMagic = Math.sqrt(magic2);
const shiftedLat = lat + (dLat * 180.0 / ((a * (1 - ee)) / (magic2 * sqrtMagic)) * Math.PI);
const shiftedLon = lon + (dLon * 180.0 / (a / sqrtMagic * Math.PI));
return { lat: shiftedLat, lon: shiftedLon };
}
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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
function transformLon(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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
return ret;
}
function isInChina(lat, lon) {
return lon >= 73.66 && lon <= 135.05 && lat >= 3.86 && lat <= 53.55;
}
GCJ02 转 BD09(百度坐标)
function gcj02ToBd09(lat, lon) {
const x_pi = Math.PI * 3000.0 / 180.0;
const z = Math.sqrt(lon * lon + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
const theta = Math.atan2(lat, lon) + 0.000003 * Math.cos(lon * x_pi);
const bdLon = z * Math.cos(theta) + 0.0065;
const bdLat = z * Math.sin(theta) + 0.006;
return { lat: bdLat, lon: bdLon };
}
BD09 转 GCJ02
function bd09ToGcj02(lat, lon) {
const x_pi = Math.PI * 3000.0 / 180.0;
const x = lon - 0.0065;
const y = lat - 0.006;
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
const gcjLon = z * Math.cos(theta);
const gcjLat = z * Math.sin(theta);
return { lat: gcjLat, lon: gcjLon };
}
完整转换工具类
class CoordTransform {
// WGS84 -> GCJ02
static wgs84ToGcj02(lat, lon) {
// 实现如上
}
// GCJ02 -> WGS84
static gcj02ToWgs84(lat, lon) {
const result = this.wgs84ToGcj02(lat, lon);
// 反向偏移
return {
lat: lat * 2 - result.lat,
lon: lon * 2 - result.lon
};
}
// GCJ02 -> BD09
static gcj02ToBd09(lat, lon) {
// 实现如上
}
// BD09 -> GCJ02
static bd09ToGcj02(lat, lon) {
// 实现如上
}
// WGS84 -> BD09 (先转 GCJ02 再转 BD09)
static wgs84ToBd09(lat, lon) {
const gcj = this.wgs84ToGcj02(lat, lon);
return this.gcj02ToBd09(gcj.lat, gcj.lon);
}
// BD09 -> WGS84 (先转 GCJ02 再转 WGS84)
static bd09ToWgs84(lat, lon) {
const gcj = this.bd09ToGcj02(lat, lon);
return this.gcj02ToWgs84(gcj.lat, gcj.lon);
}
}
// 使用
const wgs84 = { lat: 39.9042, lon: 116.4074 }; // 北京
const gcj02 = CoordTransform.wgs84ToGcj02(wgs84.lat, wgs84.lon);
const bd09 = CoordTransform.wgs84ToBd09(wgs84.lat, wgs84.lon);