Coordinate System Conversion in Tencent, Amap, and Baidu Maps: Technical Details and Engineering Practice
For developers, the practical problem is clear: GPS receivers provide WGS-84 coordinates, but displaying them directly on Chinese map services produces positioning errors. A real location's WGS-84 coordinates shown on a GCJ-02 map (such as Amap or Tencent Maps) can be off by 100–700 meters. Anyone building location features in mainland China must implement correct coordinate system conversion—between WGS-84 and GCJ-02, between GCJ-02 and BD-09—to ensure accurate positioning.
Coordinate Systems Used by Different Map Services
In practice, China's major map providers use different coordinate systems:
Tencent Maps and Amap: Both use the GCJ-02 coordinate system (also called the "Mars coordinate system") within mainland China. Their coordinates are compatible; you can overlay them directly without conversion. Outside mainland China, these services typically switch to WGS-84 or local coordinate systems without applying any offset.
Baidu Maps: Baidu uses the BD-09 coordinate system, which applies a second layer of encryption on top of GCJ-02. Because BD-09 differs slightly from GCJ-02, converting between them is necessary if you need to use Tencent or Amap coordinates on Baidu Maps (or vice versa). Baidu's API returns BD-09 coordinates by default and expects you to convert other coordinate systems to BD-09 before use. Coordinates from Baidu Maps cannot be used directly on Tencent or Amap without conversion, and the reverse also holds—proper conversion formulas are required.
Coordinate Conversion Implementation
Understanding the coordinate system differences, we can now discuss practical conversion methods. Common needs include converting WGS-84 (from GPS) to GCJ-02 (Mars coordinates), and converting GCJ-02 to BD-09 (Baidu coordinates).
Converting between WGS-84 and GCJ-02 uses an offset algorithm. Because the GCJ-02 encryption algorithm is restricted by Chinese regulations, official interfaces for converting GCJ-02 back to WGS-84 are not published. Fortunately, open-source implementations in various languages are available. The basic principle: to convert WGS-84 to GCJ-02, if the coordinate is within mainland China, the algorithm calculates offsets Δlng and Δlat (in longitude and latitude directions) and adds them to the original coordinates, yielding the encrypted GCJ-02 coordinate. To approximately reverse GCJ-02 back to WGS-84, you subtract the calculated offset. Because the offset algorithm is nonlinear, recovering exact coordinates requires iterative approximation to achieve high precision, but in typical applications, a single reverse calculation provides sufficient accuracy.
Converting between GCJ-02 and BD-09 deserves closer attention. Baidu publishes a coordinate conversion API that converts GCJ-02 (or WGS-84) to BD-09, but you can also implement the transformation using publicly available formulas. The core algorithm is straightforward, based on fixed small offsets and trigonometric transformations. Here is a Python implementation:
import math
# GCJ-02 to BD-09
def gcj02_to_bd09(lng, lat):
x = lng
y = lat
z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * math.pi)
theta = math.atan2(y, x) + 0.000003 * math.cos(x * math.pi)
bd_lng = z * math.cos(theta) + 0.0065
bd_lat = z * math.sin(theta) + 0.006
return bd_lng, bd_lat
# BD-09 to GCJ-02
def bd09_to_gcj02(bd_lng, bd_lat):
x = bd_lng - 0.0065
y = bd_lat - 0.006
z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * math.pi)
theta = math.atan2(y, x) - 0.000003 * math.cos(x * math.pi)
gcj_lng = z * math.cos(theta)
gcj_lat = z * math.sin(theta)
return gcj_lng, gcj_lat
In these formulas, the constants 0.0065 and 0.006 are the fixed offsets Baidu applies to longitude and latitude, each adding roughly six ten-thousandths of a degree; combined with the small sine terms 0.00002 and 0.000003, the transformation is smooth and introduces only minor perturbations. These constants encode the offset of BD-09 relative to GCJ-02. Using these functions, you can reliably convert GCJ-02 coordinates to BD-09, or restore Baidu coordinates back to Mars coordinates, with precision sufficient for typical mapping applications.
Common Pitfalls and Engineering Practices
In production systems, several hazards and practices bear attention:
Mixing coordinate systems: Always be explicit about which coordinate system each data source provides. GPS modules return WGS-84; Amap and Tencent APIs return GCJ-02; Baidu APIs return BD-09. Carelessly mixing these will cause positioning errors of hundreds of meters. Carefully review the coordinate system in each API's documentation and convert when necessary; do not assume compatibility.
Geographic scope: Apply GCJ-02 offset only to coordinates within mainland China. Applying Chinese offsets to coordinates outside mainland China introduces severe errors. Production code typically checks whether a coordinate lies within mainland China (using latitude and longitude bounds) before deciding whether to convert, avoiding unnecessary offset of international coordinates.
Minimize conversion steps: Avoid converting coordinates back and forth between systems. Each conversion introduces tiny floating-point errors; repeated conversions of the same point can accumulate these errors significantly. In practice, store and use original coordinates, then convert once at the final display or output step, avoiding repeated conversions of the same point.
Precision and algorithm choice: The conversion algorithms described above achieve engineering-grade precision, typically matching official conversion results to within four decimal places. Floating-point arithmetic inherently introduces minute precision loss. If very high precision is needed—for instance, reversing GCJ-02 back to WGS-84 for sensitive calculations—iterative algorithms can reach nine decimal places of precision using binary search. In most applications, however, simple conversion errors remain at the meter scale or smaller and are negligible.
Official interface limitations: Chinese map services' official APIs typically do not expose conversion from their coordinate systems back to WGS-84, due to regulatory constraints. For example, Baidu's API converts GPS (WGS-84) or GCJ-02 to BD-09, but offers no interface to convert BD-09 back to WGS-84. If you need actual GPS coordinates (WGS-84), you must implement the conversion algorithm yourself. Baidu's official documentation recommends using their published coordinate conversion interfaces rather than unofficial algorithms; however, for offline batch conversions and similar tasks, applying open algorithms achieves precision comparable to official APIs.
Map data differences: Even after correct coordinate system conversion, different map providers' underlying data can produce misalignments. Some roads or buildings may be drawn slightly differently across platforms; when you convert coordinates from one service and display them on another's map, small positioning differences may appear. This is not a conversion error but a reflection of differences in the base map data itself. Engineering practice should anticipate this and, where possible, use base maps and coordinates from the same data source to minimize discrepancy.