GCJ-02 Explained: The Chinese Coordinate System—For Developers

Written by nomadrat | Published 2020/02/18
Tech Story Tags: mapbox | china | google-maps | chinese-coordinate-system | coding-top-story | hackernoon-top-story | google-maps-china-offset-fix | gcj-02

TLDR Chinese regulators require approved map services to use standard GCJ-02 (Chinese:  Chinese: 国测局; pinyin: guó-cè-jú) Baidu went further and implemented yet another abstraction adding further obfuscation to the standard. For China users in China you must use GCJ02, for example. There is no official API to convert to WGS-84 as required by the Chinese law. It is illegal to publish a formal conversion guide for each use of this algorithm in different languages.via the TL;DR App

I remember my first visit to China in 2010. We booked a hotel in a good location in Guangzhou, saving the coordinates and routes to Google Maps prior to our arrival. But when we got there, it was immediately obvious that there were no hotels in that area. I was at the correct point where my hotel should be, but all I have around me 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 with in the hope that this might help someone.

Coordinate systems

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

Licenses

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 号».

Coordinate conversion

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.
However, you can find open-source implementations for this algorithm in different programming languages(e.g., eviltransform). In this article, i will use JavaScript implementation, but you easily will find Python, R, Go and etc. Check the Notes section of my article.
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?
I made an example with coordtransform library and Mapbox.com 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.

Which map provider to use for your users in China?

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).
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.
  • Yes: show mapbox.cn embed with GCJ-02 coordinates
  • No: show mapbox.com embed with WGS-84 coordinates
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 feedback.
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.

Written by nomadrat | i write code
Published by HackerNoon on 2020/02/18