WGS-84与火星(GCJ)坐标互转丨技术开发分享录

WGS-84与火星(GCJ)坐标互转

{{ detail.nickname }}

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

"## 一、WGS-84坐标转火星(GCJ)坐标\n\n```\nvar pi = 3.14159265358979324;\nvar a = 6378245.0;\nvar ee = 0.00669342162296594323;\n/*判断是否在国内,不在国内则不做偏移*/\nfunction outOfChina(lon, lat) {\n    if ((lon < 72.004 || lon > 137.8347) && (lat < 0.8293 || lat > 55.8271)) {\n        return true;\n    } else {\n        return false;\n    }\n}\n\nfunction transformLat(x, y) {\n    var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));\n    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;\n    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;\n    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;\n    return ret;\n}\n\nfunction transformLon(x, y) {\n    var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));\n    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;\n    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;\n    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;\n    return ret;\n}\n\nfunction transformCoord(wgLat, wgLon) {\n    var mars_point = { lon: 0, lat: 0 };\n    if (outOfChina(wgLat, wgLon)) {\n        mars_point.lat = wgLat;\n        mars_point.lon = wgLon;\n        return;\n    }\n    var dLat = transformLat(wgLon - 105.0, wgLat - 35.0);\n    var dLon = transformLon(wgLon - 105.0, wgLat - 35.0);\n    var radLat = wgLat / 180.0 * pi;\n    var magic = Math.sin(radLat);\n    magic = 1 - ee * magic * magic;\n    var sqrtMagic = Math.sqrt(magic);\n    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);\n    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);\n    mars_point.lat = wgLat + dLat;\n    mars_point.lon = wgLon + dLon;\n    return mars_point\n}\n\n// 调用\nconsole.log(transformCoord(109.812339, 35.944621))\n```\n\n## 二、火星(GCJ)坐标转WGS-84坐标\n\n```\nvar pi = 3.14159265358979324;\nvar a = 6378245.0;\nvar ee = 0.00669342162296594323;\n/*判断是否在国内,不在国内则不做偏移*/\nfunction outOfChina(lon, lat) {\n    if ((lon < 72.004 || lon > 137.8347) && (lat < 0.8293 || lat > 55.8271)) {\n        return true;\n    } else {\n        return false;\n    }\n}\n\nfunction transformLat(x, y) {\n    var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));\n    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;\n    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;\n    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;\n    return ret;\n}\n\nfunction transformLon(x, y) {\n    var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));\n    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;\n    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;\n    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;\n    return ret;\n}\n\nfunction transformCoord(wgLat, wgLon) {\n    var mars_point = { lon: 0, lat: 0 };\n    if (outOfChina(wgLat, wgLon)) {\n        mars_point.lat = wgLat;\n        mars_point.lon = wgLon;\n        return;\n    }\n    var dLat = transformLat(wgLon - 105.0, wgLat - 35.0);\n    var dLon = transformLon(wgLon - 105.0, wgLat - 35.0);\n    var radLat = wgLat / 180.0 * pi;\n    var magic = Math.sin(radLat);\n    magic = 1 - ee * magic * magic;\n    var sqrtMagic = Math.sqrt(magic);\n    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);\n    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);\n    mars_point.lat = wgLat + dLat;\n    mars_point.lon = wgLon + dLon;\n    return mars_point\n}\n\n// 调用\nconsole.log(transformCoord(109.812339, 35.944621))\n```"
PS:写作不易,如要转裁,请标明转载出处。

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

猜你想看