魔卡托与经纬度坐标的转换(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}

参考文章:https://www.jianshu.com/p/3bcc1005d435

上一篇 下一篇

猜你喜欢

热点阅读