4

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?

Sewerbird
  • 73
  • 1
  • 5

4 Answers4

13

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 People'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.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 1
    I want to use that API solution to convert google map points to Baidu map points. Can you please explain how to use that API -http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=121.4914&y=31.2423. Means what are 'from' & 'to' parameters. I have to use them in Android application(Java code) – Shirish Herwade Jun 26 '17 at 11:53
  • I just found a npm package that can convert between WGS84 and China's BD09 / GCJ02 coordinates https://www.npmjs.com/package/coordinate-convert – williamli Aug 25 '17 at 07:23
1

Companies in China are required by law not to export map data without an offset to disguise foreign companies. See https://help.openstreetmap.org/questions/37560/map-offset-in-china for more details.

Coordinates you get from maps.google.com are probably wrong while those in baidu are probably correct.

Alan Tam
  • 2,027
  • 1
  • 20
  • 33
  • 1
    I see - it seems the offset is on the city/prefecture level (Beijing having a different offset than, say, Chengdu and such), and varies. Thanks! – Sewerbird Apr 05 '15 at 04:59
1

Baidu map API does have a service to translate the Google Maps coordinates to Baidu map coordinates but I think it is only accurate for locations within China region. anywhere else is still off

Take a look at the official demo

http://developer.baidu.com/map/jsdemo.htm#a5_1

But beware there is a major delay before it returns your translated coordinate(s).

var x = 116.32715863448607;
var y = 39.990912172420714;
var ggPoint = new BMap.Point(x,y); // step 1 - create a Baidu map point from Google Maps coordinate


translateCallback = function (data){
  if(data.status === 0) {
    var marker = new BMap.Marker(data.points[0]);  // step 3 - u get the offset coordinate here
    bm.addOverlay(marker);
    bm.setCenter(data.points[0]);
  }
}

setTimeout(function(){
  var convertor = new BMap.Convertor();
  var pointArr = [];
  pointArr.push(ggPoint);
  convertor.translate(pointArr, 3, 5, translateCallback); // step 2 - pass the point as an array to the translate service
}, 1000);
Macomatic
  • 113
  • 9
  • Interesting, I have been using the api version 1.5 and coord api conversion as above, which is now broken. I'll have to give this a try. – Seano666 Nov 22 '16 at 23:44
  • Erroring out on the constructor for me. var convertor = new BMap.Convertor(); Doesn't support this method. – Seano666 Nov 23 '16 at 00:32
  • Does the given URL in my comment work on your side? You will definitely need API key to get it working. – Macomatic Nov 30 '16 at 03:28
  • API conversion was not broken after all, the issue was that the coordinates entered by my QA Dept to be mapped were USA, not China as expected. The Baidu conversion API gets angry when you try to pass that :). Appreciate your response though! – Seano666 Dec 01 '16 at 16:36
  • FYI, the coordinates that I'm working with is indeed Google Maps' coordinates. Unless you are passing non-existent x,y the Baidu API should be able to convert it. – Macomatic Dec 03 '16 at 05:24
1

My solution is

Replacing Lat and Long value in this link with current your coordinate (Google Lat and Long)

http://api.map.baidu.com/marker?location=39.916979519873,116.41004950566&output=html

phancuongviet
  • 311
  • 2
  • 11
  • Could you provide some more details on why this solution solves the problem? – Sebastian Mar 06 '20 at 09:27
  • I think this is an API which provided by Baidu Map. You can test this in original API of Baidu Map as api.map.baidu.com/lbsapi/getpoint/index.html. The result is the same with the above link. Note, please click 坐标反查 checkbox it is right side of search icon to get the hightest result. Thanks for reading my comment! – phancuongviet Mar 06 '20 at 11:25