The example of this paper tells the implementation method of Mars coordinates, Baidu coordinates, WGS84 coordinate conversion in JS. Share to everyone for reference, specific:

// Define some constant var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var pi = 3.1415926535897932384626; var a = 6378245.0; var EE = 0.00669342162296594323; / ** * Baidu coordinate system (BD-09) and Mars Coordinate system (GCJ-02) conversion * 即 Baidu turbano, Gao De * / Function BD09TOGCJ02 (BD_LON, BD_LAT) {var x_pi = 3.14159265358979324 * 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 * ie Google, Gao De Turn Baidu * / 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 * / 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 = la / 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 * / Function GCJ02TOWGS84 (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 = la / 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 * 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) + 320 * Math.sin (Lat * Pi / 30.0)) * 2.0 / 3.0; return return} function transformlng (LNG, LAAT) {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} / ** * Judgment is in China, not in China does not do offset * / function out_of_china (lng, lat) {return (LNG 137.8347) || (((((LAT)

` 55.8271) || False);} `` More about JavaScript related content can also view this station topic: "JavaScript Mathematical Computing Summary", "JavaScript Data Structure and Algorithm Skill Summary "," JavaScript Array Operation Skills Summary "and" JavaScript Character and String Operation Skill Summary " `

I hope this article will help you JavaScript programming.

© Copyright Notice

The copyright of this article belongs to the author. Please do not reprint it without permission。

THE END

Just support it if you like

## Please log in to comment

register