Conversion between Baidu coordinate (BD09), National Bureau coordinate (Mars coordinate, GCJ02), and WGS84 coordinate system _ javascript skills

Source: Internet
Author: User
This article mainly introduces the conversion data between Baidu coordinates (BD09), the space test Bureau coordinates (Mars coordinates, GCJ02), and the WGS84 coordinate system, you can refer to different coordinate systems in the project to see if the map shows a little different. The following is a conversion method written in javascript. The specific code is 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) conversion to the Mars coordinate system (GCJ-02) * Baidu to Google, autonavi * @ param bd_lon * @ param bd_lat * @ returns {* []} */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 of * Is Google, autonavi 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 to 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; 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 to WGS84 * @ param lng * @ param lat * @ returns {* []} */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 = lat/180.0 * PI; var magic = Math. sin (radlat); magic = 1-ee * 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 + 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 ret} function transformlng (lng, lat) {var ret = 300.0 + lng + 2.0 * lat + 0.1 * 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}/*** determines whether it is in China, if you are not in China, no 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 // convert the gps coordinate to the Mars coordinate var lng_lat_1 = wgs84togcj02 (113.912743, 22.497629 ); console. log ('Mars coordinate... ', lng_lat_1); // convert the Mars coordinate to the Baidu coordinate var lng_lat_2 = gcj02tobd09 (lng_lat_1 [0], lng_lat_1 [1]); console. log ('Baidu coordinate... ', 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.