Sewerbird Sewerbird - 28 days ago 5
Javascript Question

Does a Google Maps coordinate map to the same location in Baidu Maps?

I'm implementing a map view in Baidu Maps (China's primary map service) using latitude & longitude originating from Google Maps. I am finding that my locations (primarily around Shanghai) are consistently 'off' by about a kilometer, though!

Doing a linear adjustment makes the Baidu locations appear in roughly (+- a few meters) the right spot:

//Declaring my Baidu map marker's properties
...
lat : gm_location.lat + 0.00575,
lng : gm_location.lng + 0.00668,
...


Although this will do for my particular needs, I dislike these magic numbers. Anyone have any insight where this offset is emerging from?

Answer

Which Google Maps do you use, google.com/map, or google.cn/map? Satellite or street view? In all cases, the answer is no - coordinates won't correspond to the Baidu Map POI, but for slightly different reasons.

First off, since Baidu doesn't offer any useful maps outside of China, let's focus on the question being,

Does a Google Maps coordinate within China, map to the same location in Baidu Maps?

The answer is no. Baidu Maps uses its own BD-09 coordinate system, while Google Maps uses either the Chinese GCJ-02 standard (for street maps on either google.com/maps or .cn), or WGS-84 for satellite imagery on google.com satellite view maps.

GPS coordinates (WGS-84) and Google Street Map coordinates (GCJ-02) do not map to the same location on Baidu Maps, as this snippet demonstrates (I didn't include the snippet in the answer because it doesn't work due to the way SO includes scripts - but you can see the code in this other answer I gave).

enter image description here

Using a linear correction around Shanghai might work, but the values will change for other regions in China.

How to compensate for the offset

The official method for converting Google coordinates to Baidu Map coordinates is to use the Baidu Map API for doing so.

There are two versions of the API:

  1. An allegedly "server" version documented at http://developer.baidu.com/map/changeposition.htm that requires obtaining an application key

  2. A version at http://api.map.baidu.com/ag/coord/convert that's used by the Baidu Map coordinates conversion demo via http://developer.baidu.com/map/jsdemo/demo/convertor.js and doesn't need an application key.

Let's take the Google Street Map (GCJ-02) coordinates of The People's Heroes Monument in Shanghai and convert them to Baidu Maps coordinates:

curl "http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=121.4914&y=31.2423"

will output

{"error":0,"x":"MTIxLjQ5Nzk5OTg3MzM4","y":"MzEuMjQ3OTc1ODQwMTk2"}

The coordinates are base64-encoded, and decode to

121.49799987338, 31.247975840196

If map.baidu.com allowed you to enter lat/lon coordinates, you'd land at the Popele's Heroes Monument.

Baidu's API only offers conversion to BD-09.

The conversion can also be run offline, and formulas can be found on Chinese blogs. Conversion between GCJ-02, WGS-84 and B-09 can be performed using libraries like geoChina or eviltransform.