1.WGS84(4326)转墨卡托(3587)
function WGS84ToMercator(lonlat){
const coord = [];
const earthRad = 6378137.0; //地球半径
let lng = lonlat.lng * Math.PI / 180 * earthRad;
const param = lonlat.lat * Math.PI / 180;
let lat = earthRad / 2 * Math.log((1.0 + Math.sin(param)) / (1.0 - Math.sin(param)));
coord.push(lng);
coord.push(lat);
console.log('墨卡托:', coord)
return coord;
},
2.墨卡托(3857)转WGS84(4326)
function mercatorToWGS84(lonlat){
const coord = [];
const lng = lonlat.lng / 20037508.34 * 180;
let lat = lonlat.lat / 20037508.34 * 180;
lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
coord.push(lng);
coord.push(lat);
console.log('WGS84:', coord)
return coord;
}
3.测试
this.WGS84ToMercator({lng: 113, lat: 23})
this.mercatorToWGS84({lng: 12604705.942522367, lat: 2632018.637586424})
4.结果