Skip to content

Instantly share code, notes, and snippets.

@fulippo
Last active August 18, 2018 08:48
Show Gist options
  • Save fulippo/357bb775b676ee073e0a to your computer and use it in GitHub Desktop.
Save fulippo/357bb775b676ee073e0a to your computer and use it in GitHub Desktop.
Convert coords from Google Maps (WGS-84) to Baidu Maps (BD-09)
/**
* Convert longitude and latitude from standard WGS-84 system
* to Baidu's BD-09
*
* @see http://stackoverflow.com/a/29512814/718811 for a detailed
* description of the problem.
*
* @param float lng Longitude
* @param float lat Latitude
* @return object Converted coords
*/
function convertCoords(lng, lat){
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
var x = lng, y = lat;
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 bd_lon = z * Math.cos(theta) + 0.0065;
var bd_lat = z * Math.sin(theta) + 0.006;
return {lng: bd_lon, lat:bd_lat};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment