Mars coordinates, Baidu coordinates, WGS84 coordinate conversion code (JS version)

Source: Internet
Author: User

/** * Provides the conversion between Baidu coordinates (BD09), National geodetic coordinates (MARS coordinates, GCJ02), and WGS84 coordinate system*///Define some constantsvarX_PI = 3.14159265358979324 * 3000.0/180.0;varPI = 3.1415926535897932384626;varA = 6378245.0;varEE = 0.00669342162296594323;/** * Baidu coordinate system (BD-09) and Mars coordinate system (GCJ-02) conversion * That is, Baidu to Google, German * @param bd_lon * @param bd_lat * @returns {*[]}*/functionbd09togcj02 (Bd_lon, Bd_lat) {varX_PI = 3.14159265358979324 * 3000.0/180.0; varx = bd_lon-0.0065; vary = bd_lat-0.006; varz = MATH.SQRT (x * x + y * y)-0.00002 * Math.sin (Y *x_pi); vartheta = math.atan2 (y, x)-0.000003 * MATH.COS (x *x_pi); varGG_LNG = Z *Math.Cos (theta); varGg_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 {*[]}*/functiongcj02tobd09 (LNG, LAT) {varz = MATH.SQRT (LNG * LNG + lat * lat) + 0.00002 * Math.sin (LAT *x_pi); vartheta = math.atan2 (lat, LNG) + 0.000003 * MATH.COS (LNG *x_pi); varBD_LNG = Z * Math.Cos (theta) + 0.0065; varBd_lat = Z * Math.sin (theta) + 0.006; return[BD_LNG, Bd_lat]}/** * WGS84 to GCJ02 * @param LNG * @param lat * @returns {*[]}*/functionwgs84togcj02 (LNG, LAT) {if(Out_of_china (LNG, LAT)) {return[LNG, LAT]}Else {        varDlat = Transformlat (lng-105.0, lat-35.0); varDLNG = TRANSFORMLNG (lng-105.0, lat-35.0); varRadlat = lat/180.0 *PI; varMagic =Math.sin (Radlat); Magic= 1-ee * Magic *Magic; varSqrtmagic =math.sqrt (Magic); Dlat= (Dlat * 180.0)/((A * (1-ee))/(Magic * sqrtmagic) *PI); DLNG= (DLNG * 180.0)/(A/sqrtmagic * MATH.COS (Radlat) *PI); varMglat = Lat +Dlat; varMGLNG = LNG +dlng; return[MGLNG, Mglat]}}/** * GCJ02 converted to WGS84 * @param LNG * @param lat * @returns {*[]}*/functiongcj02towgs84 (LNG, LAT) {if(Out_of_china (LNG, LAT)) {return[LNG, LAT]}Else {        varDlat = Transformlat (lng-105.0, lat-35.0); varDLNG = TRANSFORMLNG (lng-105.0, lat-35.0); varRadlat = lat/180.0 *PI; varMagic =Math.sin (Radlat); Magic= 1-ee * Magic *Magic; varSqrtmagic =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]}}functionTransformlat (LNG, LAT) {varRET = -100.0 + 2.0 * LNG + 3.0 * LAT + 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; returnret}functiontransformlng (LNG, LAT) {varRET = 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; returnret}/** * To determine whether in the domestic, not in the country do not offset * @param LNG * @param lat * @returns {Boolean}*/functionOut_of_china (LNG, LAT) {return(LNG < 72.004 | | LNG > 137.8347) | | (Lat < 0.8293 | | | lat > 55.8271) | |false);}

Mars coordinates, Baidu coordinates, WGS84 coordinate conversion code (JS version)

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.