Conversion between Baidu coordinates (BD09), geodetic coordinates (MARS coordinates, GCJ02), and WGS84 coordinate system (JS version code)

Source: Internet
Author: User
Tags cos sin

/** * Created by Wandergis on 2015/7/8. * provided Baidu coordinates (BD09), geodetic coordinates (MARS coordinates, GCJ02), and WGS84 coordinate system between the conversion *///define some constants var X_pi = 3.14159265358979324 * 3000.0/180.0;var PI = 3.1415 926535897932384626;var a = 6378245.0;var ee = 0.00669342162296594323;/** * Baidu coordinate system (BD-09) and Mars coordinate system (GCJ-02) conversion * That is, Baidu to Google, high De * @param bd_lon * @param bd_lat * @returns {*[]} */function bd09togcj02 (Bd_lon, Bd_lat) {var x_pi = 3.14159265358979    324 * 3000.0/180.0;    var x = bd_lon-0.0065;    var y = bd_lat-0.006;    var z = math.sqrt (x * x + y * y)-0.00002 * Math.sin (y * x_pi);    var theta = math.atan2 (y, x)-0.000003 * MATH.COS (x * x_pi);    var gg_lng = z * Math.Cos (theta);    var Gg_lat = z * Math.sin (theta); return [GG_LNG, gg_lat]}/** * Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) conversion * That is, Google, High German to Baidu * @param LNG * @param lat * @returns {*[]} *    /function gcj02tobd09 (LNG, lat) {var z = math.sqrt (LNG * LNG + lat * lat) + 0.00002 * Math.sin (LAT * x_pi);   var theta = math.atan2 (lat, LNG) + 0.000003 * Math.Cos (LNG * x_pi); var bd_lng = z * Math.Cos (theta) + 0.0065;    var Bd_lat = z * Math.sin (theta) + 0.006;    return [BD_LNG, bd_lat]}/** * WGS84 turn GCj02 * @param LNG * @param lat * @returns {*[]} */function wgs84togcj02 (LNG, LAT) { if (Out_of_china (LNG, LAT)) {return [LNG, LAT]} else {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]}}/** * GCJ02 convert to WGS84 * @param LNG * @param lat * @returns {*[]} */function gcj02towgs84 (LNG, L at) {if (Out_of_china (LNG, LAT)) {return [LNG, LAT]} else {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);        Mglat = lat + Dlat;        MGLNG = LNG + dlng; return [LNG * 2-mglng, LAT * 2-mglat]}}function Transformlat (LNG, LAT) {var ret = -100.0 + 2.0 * LNG + 3.0 * L    at + 0.2 * lat * lat + 0.1 * LNG * LAT + 0.2 * MATH.SQRT (Math.Abs (LNG));    RET + = (20.0 * Math.sin (6.0 * LNG * pi) + 20.0 * Math.sin (2.0 * LNG * 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) + * * Math.sin (LAT * pi/30.0)) * 2.0/3.0; Return ret}function transformlng (LNG, LAT) {var ret = 300.0 + LNG + 2.0 * LAT+ 0.1 * LNG * LNG + 0.1 * LNG * LAT + 0.1 * MATH.SQRT (Math.Abs (LNG));    RET + = (20.0 * Math.sin (6.0 * LNG * pi) + 20.0 * Math.sin (2.0 * LNG * pi)) * 2.0/3.0;    RET + = (20.0 * Math.sin (LNG * pi) + 40.0 * Math.sin (lng/3.0 * pi)) * 2.0/3.0;    RET + = (150.0 * Math.sin (lng/12.0 * pi) + 300.0 * Math.sin (lng/30.0 * pi)) * 2.0/3.0; Return ret}/** * Determine whether in the domestic, not in the domestic do not do the offset * @param LNG * @param lat * @returns {boolean} */function Out_of_china (LNG, LAT) {R Eturn (LNG < 72.004 | | LNG > 137.8347) | | (Lat < 0.8293 | | lat > 55.8271) | | false);}

  

Conversion between Baidu coordinates (BD09), geodetic coordinates (MARS coordinates, GCJ02), and WGS84 coordinate system (JS version code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.