js wgs84转gcj02方法
时间: 2023-09-01 08:09:55 浏览: 135
WGS84坐标系是一种地球坐标系,而GCJ02坐标系则是中国国家测绘局制定的一种坐标系,用于国内地图服务。以下是JavaScript实现WGS84转GCJ02的方法:
```
function wgs84ToGcj02(lng, lat) {
var pi = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var dLat = transformLat(lng - 105.0, lat - 35.0);
var dLng = transformLng(lng - 105.0, lat - 35.0);
var radLat = lat / 180.0 * pi;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
var mgLat = lat + dLat;
var mgLng = lng + dLng;
return [mgLng, mgLat];
}
function transformLat(lng, lat) {
var pi = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var radLat = lat / 180.0 * pi;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
return (lat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
}
function transformLng(lng, lat) {
var pi = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var radLat = lat / 180.0 * pi;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
var dLng = (lng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
return dLng;
}
```
其中,`lng`和`lat`参数分别代表WGS84坐标系下的经度和纬度,函数返回GCJ02坐标系下的经纬度,以数组形式返回。
阅读全文