经纬度与墨卡托坐标互转丨技术开发分享录

经纬度与墨卡托坐标互转

{{ detail.nickname }}

转载 翻译 {{ formatTime(detail.create_time) }} 字数 {{ detail.content && detail.content.length }} 阅读 {{ detail.read_num }} {{ formatTag(v) }}

"## 一、经纬度转墨卡托\n\n```\nfunction lonlatTomercator(lonlat) {\n    var mercator = { x: 0, y: 0 };\n    var x = lonlat.x * 20037508.34 / 180;\n    var y = Math.log(Math.tan((90 + lonlat.y) * Math.PI / 360)) / (Math.PI / 180);\n    y = y * 20037508.34 / 180;\n    mercator.x = x;\n    mercator.y = y;\n    return mercator;\n}\n\nconsole.log(lonlatTomercator({x: 117.722495, y: 28.209988})) // {x: 13104808.19648949, y: 3275474.3604030455}\n```\n\n## 一、墨卡托转经纬度\n\n```\nfunction mercatorTolonlat(mercator) {\n    var lonlat = { x: 0, y: 0 };\n    var x = mercator.x / 20037508.34 * 180;\n    var y = mercator.y / 20037508.34 * 180;\n    y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);\n    lonlat.x = x;\n    lonlat.y = y;\n    return lonlat;\n}\nconsole.log(mercatorTolonlat({x: 13104808.19648949, y: 3275474.3604030455})) // {x: 117.72249500000001, y: 28.209987999999978}\n```\n\n>[warning] 上面转化算法可以用,但可能不太精确"
PS:写作不易,如要转裁,请标明转载出处。

如果此篇对您有帮助,可小额赞助,以兹鼓励!

猜你想看