/** 地理坐标转墨卡托 */
function convertToMercator(lonLat) {
var D2R = Math.PI / 180, A = 6378137, MAXEXTENT = 20037508342789244e-9;
var adjusted = Math.abs(lonLat[0]) <= 180 ? lonLat[0] : lonLat[0] - sign(lonLat[0]) * 360;
var xy = [
A * adjusted * D2R,
A * Math.log(Math.tan(Math.PI * 0.25 + 0.5 * lonLat[1] * D2R))
];
if (xy[0] > MAXEXTENT)
xy[0] = MAXEXTENT;
if (xy[0] < -MAXEXTENT)
xy[0] = -MAXEXTENT;
if (xy[1] > MAXEXTENT)
xy[1] = MAXEXTENT;
if (xy[1] < -MAXEXTENT)
xy[1] = -MAXEXTENT;
return xy;
}
/** 墨卡托转地理坐标 */
function convertToWgs84(xy) {
var R2D = 180 / Math.PI;
var A = 6378137;
return [
xy[0] * R2D / A,
(Math.PI * 0.5 - 2 * Math.atan(Math.exp(-xy[1] / A))) * R2D
];
}
摘自turf