A More Complete iOS Solution to the China GPS Offset Problem

This is a guest post by a friend, [unnamed for now]. It goes quite in-depth into China’s GPS issue, which I’ve complained about here before. The hope is that, armed with the following information, non-Chinese developers will be able to get around the issue more quickly and more effectively. Note that while the information below was applied to iOS app development, it isn’t strictly iOS-specific.


Description of the Problem

One problem that often comes up when people stay in China for an extended period of time is that they find their GPS devices don’t work. Sure your iPhone or Android phone will report your own location just fine, but try using a route tracking feature when you’re jogging or if you use an app showing other people’s GPS locations like Find My Friends, you’ll likely see they’re standing in a river or some place 500 meters away even if they’re standing right next to you.

This is the mysterious China GPS offset problem. This has been covered in a few posts [in Chinese] here, here, and here. Basically the Chinese government strictly controls mapping data within China. It’s illegal to map or create GPS traces within China without authorization. There have been stories of a few foreigners who created hiking trails near sensitive buildings w/ GPS devices being arrested due to relevant local laws.

For popular map apps such as Google Maps or Apple Maps on iOS, the user’s own location will be correct. This is because licensed companies that register with the government will be given the corrected algorithm to adjust the user’s position. Google Maps, Bing and others allow you to search for a location based on the GPS coordinates, but no local Chinese map providers such as Baidu Maps allow you to.

If you had taken a photo near the Forbidden City, load the photo into iPhoto or Picasa and look at where it is on the map you’ll see the location is just a bit off, 300-500 meters and typically about a block or two away. Not far enough to be extremely inaccurate but incorrect enough to annoy and not place you in the proper position.

Baidu Maps offset example

Fig 1. Example of proper position at Xujiahui and the offset location to the northwest

Two GPS standards

The most common GPS standard used internationally is based on a coordinate system called WGS-84. The globe is an imperfect sphere and any mapping from 3D to 2D introduces some compromises. People who get really into it will note that as you get further away from the equator, the way GPS coordinates for latitude and longitude change aren’t the same even if you’re traveling the same distance. However this is the GPS we’ve come to know and is used globally.

China uses a standard called GCJ-02 which is based off an older Soviet system of coordinates introduced in the 1940’s. It’s converting from WGS-84 to GCJ-02 that we’d like to accomplish. Chinese programmers refer to this coordinate system as the 火星坐标系统 or “Mars coordinate system” (as in you’re mapping from Earth with WGS-84 to Mars in GCJ-02).

Preliminary tries to correct the problem

Static offset

The first tries in the English-language world to correct for this China offset problem noticed that in local areas like within the city of Shanghai or Beijing, the difference was relatively fixed. That is, if you just subtract a few degrees from the latitude and add a few for longitude, you can correct the position. They quickly realized that the translation was non-linear, though, changing from city to city.

Collecting data points

Approaching this problem myself, I found out that as long as I was within China’s IP range, I would see the iOS simulator report my simulated location correctly, but if I dropped a pin on the same GPS coordinates it would be off. I created a simple app that let you drag the pin back to your real location and after scraping Wikipedia’s list of cities in China, had 657 data points.

Data_points

Fig 2. 657 points taken from list of official cities in China from Wikipedia

Using Excel’s LINEST function you can split the data up into groups and actually get a pretty decent correction that works across the whole country although it will still be off by a few meters. Enough to put you across the street from where you really were or down a few stores.

google data

Fig 3. Example of Google data point set with offsets

It turns out if you search in Chinese, several people sell massive data sets of tens of thousands of points within China with their corresponding offset. Apparently people have run into this need before. On Taobao you can find sets from 400 RMB to 900 RMB.

datasets for sale 3

Fig 4. Example of data set for sale on Taobao

Hints at already solved code

A few English language posts stated that Chinese Android coders had already released the proper algorithm in open source. After several searches in Chinese, finding relevant posts was easy. But the actual ones that solve the problem took more hunting until I found the personal website of Rover Tang and a post on a popular tech site called XCoder.cn.

Solution found and explained

Keeping it brief, the originally released code was a C file that took into account all sorts of height, GPS time and date etc. even though they were unused. This could be found several places online. A refactored and cleaner version of the code is available in C# on EvilTransform.cs.

It’s basically a complicated transform using equations describing an ellipsoid (what the Earth is) from one system of coordinates to another. Once you throw in GPS in WGS-84 you get the same ones back in GCJ-02.

You’ll note that the code interprets that anything within China needs this conversion, anything outside of China, doesn’t. And that China is defined as anything between Latitude 0.83 to 56 and Longitude 72 to 138. I think there’s a few countries caught in that rectangle that might object.

So what now?

So now any web or mobile app developers who need to record GPS paths, post GPS locations, or anything else on top of a map can now have the proper locations. It was a huge relief to me to finally find a solution that works anywhere in China so we can all go back to creating apps that work.


References

7 Comments to “A More Complete iOS Solution to the China GPS Offset Problem

  1. Manu says:

    Fascinating. I put the correction algorithm into python to make it usable for OSX and Linux-users as well.

    http://blog.snapdragon.cc/china-gps-offset-problem/

  2. Shane says:

    This is very cool. Should have known someone would be able to figure this out. Now if only the satellite view on Google Maps matched up with the map view.

    Hopefully this will be found and used by other map using app coders!

  3. Flo says:

    Amazing, this is a very helpful post, thank you!

  4. Brandon says:

    Has anyone tried entering WGS-84 coordinates and displaying the result on Google Maps in the States?

  5. James Bayliss-Smith says:

    Hi there great post and the best explanation I’ve found in the web so far. What I’m still puzzled about is why this is the case. Is it because google maps somehow collaborates with the Chinese government. Surely the GPS is accurate as it comes from US Satellites so why don’t google just use maps that correspond with that and cut the Chinese out of the equation. Also many posts state that satellite view is correct but the street view is incorrect. I’ve found this to be the other way round on google maps for iPad (quite new I believe). On my iphone it seems to be correct in both views on apple and google maps.

    Now I dare say there won’t be a logical answer to this but Why Oh Why! This is nonsensical if the US military wanted to bomb China they won’t be relying on google maps. Either this is completely paranoid and illogical (wouldn’t surprise me at all in China) o there must b some commercial reason behind this that benefits Chinese companies. Any explanation much appreciated particularly in regard to the google collaboration but. Cheers

  6. James Bayliss-Smith says:

    Okay slight correction. Google maps on my iPhone also displaying wrong location in Satellite view. Apple Maps perfect. I guess any potential wrongdoer should just get an iPhone to beat China’s paranoid mapping bureau

  7. XHC says:

    Thanks a lot . The code works really well. I also find a project on github which looks like ported from java.

Leave a Reply

Sinosplice and all material found herein © 2002-2014, John Pasden. All rights reserved.
Sinosplice is happily hosted by WebFaction. Design by Dao By Design