43°

javascript 实现wgs84 坐标转百度坐标

 const PI = 3.14159265358979324;
 const a = 6378245.0;
 const ee = 0.00669342162296594323;
 const x_pi = 3.14159265358979324 * 3000.0 / 180.0;

// wgs84坐标转百度坐标 function wgs2bd(lat, lon) {        _wgs2gcj = wgs2gcj(lat, lon);        _gcj2bd = gcj2bd(_wgs2gcj[0], _wgs2gcj[1]);        return _gcj2bd; }

function gcj2bd(lat, lon) {        x = lon, y = lat;        z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);        theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);        bd_lon = z * Math.cos(theta) + 0.0065;        bd_lat = z * Math.sin(theta) + 0.006;        return [ bd_lat, bd_lon ]; }

function bd2gcj(lat, lon) {        x = lon - 0.0065, y = lat - 0.006;        z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);        theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);        gg_lon = z * Math.cos(theta);        gg_lat = z * Math.sin(theta);        return [ gg_lat, gg_lon ]; }

function wgs2gcj(lat, lon) {        dLat = transformLat(lon - 105.0, lat - 35.0);        dLon = transformLon(lon - 105.0, lat - 35.0);        radLat = lat / 180.0 * PI;        magic = Math.sin(radLat);        magic = 1 - ee * magic * magic;        sqrtMagic = Math.sqrt(magic);        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);        mgLat = lat + dLat;        mgLon = lon + dLon;        return [ mgLat, mgLon ]; }

function transformLat(lon,lat) {        ret = -100.0 + 2.0 * lon + 3.0 * lat + 0.2 * lat * lat + 0.1 * lon * lat + 0.2 * Math.sqrt(Math.abs(lon));        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI  / 30.0)) * 2.0 / 3.0;        return ret; }

function transformLon(lon,lat) {        ret = 300.0 + lon + 2.0 * lat + 0.1 * lon * lon + 0.1 * lon * lat + 0.1 * Math.sqrt(Math.abs(lon));        ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;        ret += (20.0 * Math.sin(lon * PI) + 40.0 * Math.sin(lon / 3.0 * PI)) * 2.0 / 3.0;        ret += (150.0 * Math.sin(lon / 12.0 * PI) + 300.0 * Math.sin(lon / 30.0 * PI)) * 2.0 / 3.0;        return ret; }

 

本文由【jingshishengxu】发布于开源中国,原文链接:https://my.oschina.net/jingshishengxu/blog/3159465

全部评论: 0

    我有话说: