Thanks to Chris Wu from Mapbox China for his feedback on this article and insights about how maps works in China. Without him this article would not be complete.
I remember my first visit to China in 2010. We booked a hotel in a good location in Guangzhou and saved coordinates and routes to Google Maps. But when we arrived at a destination, it was obvious that there are no hotels. I was at the point where my hotel should be, but all i have around is a huge square(花城广场 aka Flower City Square). Long story short. At some point, some guy saved us with his Meizu phone by showing that our hotel actually a few miles away from this square.
These days i work with China and face the same challenges as a developer. I decided to write an article to summarize my experience in the hope that this might help someone.
For starters let's talk about the main coordinate systems used today. An international standard is WGS-84(Earth coordinates) or World Geodetic System, where 84 is the latest revision of this standard applied in 1984. This standard is used by GPS. Each time you use Google Maps(or almost any maps) you can thank WGS-84. Now let's talk about China.
WGS-84 ⇛ GCJ-02 ⇛ BD-09
Chinese regulators require approved map services to use standard GCJ-02(Chinese: 国测局; pinyin: guó-cè-jú). It was formulated by the Chinese State Bureau of Surveying and Mapping in 2002. Sometimes GCJ-02 is called Mars coordinates. GCJ-02 is based on WGS-84 and uses an obfuscation algorithm, which adds random offsets to both the latitude and longitude to improve national security. If the WGS-84 point is placed on the GCJ-02 map, on average, there will be a location offset for about 300-500m. Very few are into the «miles» apart, usually just by a couple of blocks. That's why i got lost during my first visit to China.
Hold tight. There is more. Baidu went further and implemented yet another abstraction adding further obfuscation to GCJ-02. It's called the BD-09(Baidu, formulated in 2009). The primary purpose was to protect Baidu's data so that competitors couldn't easily duplicate. Baidu has internal tools to shift between the three coordinate systems, but only making GCJ-02 to BD-09 public to encourage using their standard for tiles, search coordinates, etc.
As required by the Chinese law there is no official API to convert to WGS-84.
According to Wikipedia, «OpenStreetMap, the crowdsourced project to assemble a map of the world, advises that "private surveying and mapping activities are illegal in China». Although if you look at the Baidu or Mapbox maps, you'll often find OSM attribution next to the map license. It means that the usage of OSM is not illegal in China, and you can add data to OSM from satellite images. But to do ground data collection, you must obtain a Survey License(ability to collect data and create a commercial product). And it's a whole different story.
As Chris mentioned to me, a survey license is extremely difficult to get. As a result, there are fewer than 20 companies that have this license. Giant tech companies like Alibaba needed to acquire AutoNavi just for this license, Tencent had to buy NavInfo. Mapbox, for example, partnered with EMG, a leader in navigation grade map data. Didi was the first company to receive its license directly in the past decade(quite a milestone). Map license, on the other hand, is easier to obtain — if you look at the bottom right of licensed base maps in China (Mapbox, Baidu, AutoNavi, etc.), you'll see something like «GS (2020) 123 号».
Baidu map license
As you can imagine, conversion goes like this: WGS-84 ⇛ GCJ-02 ⇛ BD-09 and backward. It is illegal to publish a formal conversion guide — each use of the coordinate shifting needs to be approved by SBSM.
Our Chinese office uses Baidu Get Point Service to get BD-09 coordinates of the desired object. A screenshot is below. I then store this coordinates in the database and do conversion towards WGS-84 or GCJ-02, based on the user geolocation. For users in China you must use GCJ-02, remember?
BD-09 from http://api.map.baidu.com/lbsapi/getpoint/
I made an example with coordtransform library and Mapbox for you to play around. Copy BD-09 coordinates from this service and paste them here to see the result of transformation.
You can find source code for this example here
Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.
If you want to provide a smooth map experience to your users from Mainland China, you don't have a lot of options. Google Maps isn't working without a VPN. I love Baidu Maps in China. They provide excellent UI with a lot of handy GEO-data. I mean, look at the details.
But when it comes to the point where you should embed a map on your web page, the only choice left for me is Mapbox. Mapbox is an excellent alternative map service. It's legal and works perfectly fine without a VPN in China. Due to Chinese regulations, they serve 3 licensed base maps through mapbox.cn. All data inside China is shifted (from their mapping partner), all data outside China is unshifted (from their own data sets, including OSM).
Mapbox with a WGS-84 113.31924878632225, 23.12426152397713 coordinates (花城广场 in Guangzhou)
I tested api.mapbox.com endpoint with a global access token, and it worked perfectly fine from Guangzhou. But in the case with China, it's a gamble, so i suggest to obtain the access token from mapbox.cn.
Here is an algorithm for you. Is your visitor GeoIP in China?
I hope this post will save time for someone. I will probably add more information later since this is a complex and not well-understood topic. Drop me a line if you have any questions or suggestions.