1.EPSG:3857转EPSG:4326
/**
* EPSG:3857转EPSG:4326
* @param lonlat {lon:xxx,lat:xxx}
*/
function longlatToMercator(lonlat) {
const mercator = { x: 0, y: 0 };
const earthRad = 6378137.0;
mercator.x = ((lonlat.lon * Math.PI) / 180) * earthRad;
const a = (lonlat.lat * Math.PI) / 180;
mercator.y = (earthRad / 2) * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
return mercator;
}
2.EPSG:4326转EPSG:3857
/**
* EPSG:4326转EPSG:3857
* @param mercator {x:xxx,y:xxx}
*/
function mercatorTolonlat(mercator) {
const lonlat = { x: 0, y: 0 };
const x = (mercator.x / 20037508.34) * 180;
let y = (mercator.y / 20037508.34) * 180;
y = (180 / Math.PI) * (2 * Math.atan(Math.exp((y * Math.PI) / 180)) - Math.PI / 2);
lonlat.x = x;
lonlat.y = y;
return lonlat;
}
请大家多多指教…