WGS84 转换成 GCJ02坐标系web代码
时间: 2023-07-03 16:31:05 浏览: 243
以下是使用 JavaScript 实现 WGS84 转换成 GCJ02 坐标系的代码示例:
```javascript
// 定义一些常量
const pi = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
// 转换函数
function transform(wgLat, wgLon) {
let dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
let dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
let radLat = wgLat / 180.0 * pi;
let magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
let sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
let lat = wgLat + dLat;
let lon = wgLon + dLon;
return {
lat: lat,
lon: lon
};
}
function transformLat(x, y) {
let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
function transformLon(x, y) {
let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
```
你可以通过调用 `transform` 函数来将 WGS84 坐标系的经纬度转换为 GCJ02 坐标系。例如:
```javascript
let wgLat = 31.227000;
let wgLon = 121.481421;
let gcj = transform(wgLat, wgLon);
console.log(gcj); // 输出 {lat: 31.223298186446436, lon: 121.48710787823406}
```
注意,这里的经纬度是以度为单位的。
阅读全文