美文网首页
魔卡托与经纬度坐标的转换(javascript)

魔卡托与经纬度坐标的转换(javascript)

作者: 南瓜pump | 来源:发表于2023-03-22 18:41 被阅读0次
    • 魔卡托转经纬度
    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

    相关文章

      网友评论

          本文标题:魔卡托与经纬度坐标的转换(javascript)

          本文链接:https://www.haomeiwen.com/subject/bdrbrdtx.html