魔卡托与经纬度坐标的转换(javascript)
2023-03-22 本文已影响0人
南瓜pump
- 魔卡托转经纬度
function mercatorTolonlat(xx,yy){
let lonlat={};
let x = xx/20037508.34*180;
let y = yy/20037508.34*180;
y= 180/Math.PI*(2*Math.atan(Math.exp(y*Math.PI/180))-Math.PI/2);
lonlat.lon = x;
lonlat.lat = y;
return lonlat;
};
- 经纬度转魔卡托
function lonlatToMercator(lon,lat){
var mercator = {};
let x = lon * 20037508.34 / 180;
let y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
mercator.x = x;
mercator.y = y;
return mercator;
}
- 调用测试(直接在浏览器控制台即可进行调试)
调用:mercatorTolonlat(13258007.674267704,3797781.6357488926);
输出结果:{lon: 119.09870932426455, lat: 32.262228563514476}
调用:lonlatToMercator(119.098709,32.26222856);
输出结果:{x: 13258007.63817074, y: 3797781.6352862334}