The conversion _javascript technique between Baidu coordinates (BD09), national survey coordinates (MARS coordinates, GCJ02), and WGS84 coordinates

Source: Internet
Author: User
Tags abs cos sin

In the project to face a different coordinate system, on the map to show more or less will be somewhat biased, the following is the use of JavaScript to write the conversion method, the specific code as follows:

 Define some constants 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 the Mars coordinate system (GCJ-02) conversion * That is, Baidu to Google, Gould * @param bd_lon * @param bd_lat * @returns {*[]}/F
  Unction 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 * Namely Google, high Tak 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
TA = 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 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 = t
RANSFORMLNG (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 conversion 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 * 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; 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}/** * To determine whether at home, not at home do not offset * @param LNG * @param lat * @returns {Boolean}/function Out_of_china (LNG, LAT) { Return (LNG < 72.004 | | LNG > 137.8347) | |
(Lat < 0.8293 | | | lat > 55.8271) | | false);
///Use example//GPS coordinates to Mars coordinate var lng_lat_1 = wgs84togcj02 (113.912743,22.497629);
Console.log (' Mars coordinates ... ', lng_lat_1);
Mars coordinates to Baidu coordinate var lng_lat_2 = gcj02tobd09 (Lng_lat_1[0], lng_lat_1[1]); Console.log (' Baidu coordinates ... ', lng_lat_2);

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.