Customizing the Traccar Tracking App (Part 3): Integrating the AMap Location SDK + Coordinate System Conversion
In the previous two posts I added a Fused Provider and dynamic report frequency to the Traccar Android client. It works well outdoors, but indoors there’s no GPS signal, so it can only rely on network positioning, and I found that Android’s native network positioning is only mediocre in China. Especially inside large shopping malls, it’s clearly less accurate than AMap (Gaode Maps), the map app I commonly use.
So this time I decided to integrate the AMap location SDK. One thing to watch out for is that AMap returns coordinates in the GCJ-02 coordinate system, while the Traccar server uses WGS84, so I need to convert them to WGS84 before reporting. Otherwise, if GCJ-02 coordinates were sent directly to the server, the track history would be offset when viewed later.
Why AMap Is More Accurate in China
Network positioning works by having the phone scan nearby cell towers and Wi-Fi APs and send information such as cell IDs, Wi-Fi MAC addresses and signal strengths to a server, which compares them against the positions recorded in its database to work out an approximate location. So how accurate it is mainly depends on how complete the database is.
For Android’s native NETWORK_PROVIDER, a phone with GMS uses Google’s database, and Google’s database has incomplete information on cell towers in China. A phone running a Chinese ROM usually uses the network location service provided by the manufacturer, and the results vary from brand to brand. AMap has a lot of users in China, its database of cell towers and Wi-Fi APs is fairly complete, and its indoor positioning accuracy is relatively high, so I simply integrated AMap’s location SDK directly.
Applying for an AMap API Key
Before using the AMap SDK, I first need to register an account on the AMap Open Platform, create an application in the console, and then add a Key to it. For the platform I choose Android and fill in the app’s package name (PackageName) and the SHA1 of the signing certificate (release and debug can be entered separately).
Adding the amap Variant
The original Traccar already has two variants, regular and google: regular uses Android’s native positioning and google uses Google Play services positioning. I added an amap variant, and the AMap SDK is only included in this variant, so the other versions aren’t affected.
Integrating the AMap Location SDK
I just use it as described in AMap’s official documentation: I configure the Key I applied for, call the privacy compliance APIs before creating the AMapLocationClient, and then start continuous positioning. When a position comes back, its GCJ-02 coordinates are first converted to WGS84, and since AMapLocation itself extends Android’s Location, it can then be handed straight to the original reporting logic, without changing any of the code after that.
There are also two parameters that are important and easy to overlook:
- Set isMockEnable to false to reject mock locations; the default is true
- Change the communication protocol (AMapLocationProtocol) to HTTPS, which is more secure because a man-in-the-middle can’t eavesdrop on the location information, and after all, location is sensitive personal data; the default is HTTP
Converting Between GCJ-02 and WGS84
The coordinates returned by the AMap SDK are in GCJ-02, commonly known as “Mars coordinates”, a coordinate system defined by China’s State Bureau of Surveying and Mapping in 2002. It adds a nonlinear offset on top of WGS84, so the offset differs from place to place. Out of curiosity, I calculated and plotted the offset for major Chinese cities (how far off the GCJ-02 latitude and longitude would be if plotted on a map as WGS84). It’s roughly two or three hundred meters to over six hundred meters, mainly toward the east:

My code and the Traccar server both use WGS84 throughout, so the GCJ-02 coordinates from AMap need to be converted back to WGS84 before reporting, otherwise the whole track would be shifted by several hundred meters. Here’s how:
Forward: the official WGS84 to GCJ-02 algorithm has never been published, but the version circulating online is already quite mature. It first uses a bunch of polynomials and sine functions to compute an offset in meters, then converts that into latitude and longitude offsets using the parameters of the Krasovsky ellipsoid (semi-major axis a = 6378245, eccentricity squared ee = 0.00669342162296594323), and adds them to the WGS84 coordinates.
Reverse: going the other way, there’s no formula that directly converts GCJ-02 back to WGS84, because the offset is computed from the WGS84 coordinates, and all I have is the GCJ-02 coordinates. I used the simplest approximation: plug the GCJ-02 coordinates straight in to compute the offset, then subtract it. The two coordinates are only a few hundred meters apart, and the offset doesn’t change much over such a small range. I verified it against the formula: the error for major Chinese cities is between 0.3 and 2.4 m, and under 5 m at most, much smaller than the phone’s own positioning error, so it can be ignored. If I need more precision, I can keep iterating: convert the computed WGS84 forward to GCJ-02 again, compare it with the original GCJ-02, and correct by the difference. After 3 iterations the error gets close to 0. The core code is as follows:
fun convert(gcjLat: Double, gcjLon: Double): DoubleArray {
var dLat = transformLat(gcjLon - 105.0, gcjLat - 35.0)
var dLon = transformLon(gcjLon - 105.0, gcjLat - 35.0)
val radLat = gcjLat / 180.0 * Math.PI
var magic = sin(radLat)
magic = 1 - EE * magic * magic
val sqrtMagic = sqrt(magic)
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * Math.PI)
dLon = (dLon * 180.0) / (A / sqrtMagic * cos(radLat) * Math.PI)
val wgsLat = gcjLat - dLat
val wgsLon = gcjLon - dLon
return doubleArrayOf(wgsLat, wgsLon)
}
private fun transformLat(x: Double, y: Double): Double {
var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
ret += (20.0 * sin(6.0 * x * Math.PI) + 20.0 * sin(2.0 * x * Math.PI)) * 2.0 / 3.0
ret += (20.0 * sin(y * Math.PI) + 40.0 * sin(y / 3.0 * Math.PI)) * 2.0 / 3.0
ret += (160.0 * sin(y / 12.0 * Math.PI) + 320.0 * sin(y * Math.PI / 30.0)) * 2.0 / 3.0
return ret
}
private fun transformLon(x: Double, y: Double): Double {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
ret += (20.0 * sin(6.0 * x * Math.PI) + 20.0 * sin(2.0 * x * Math.PI)) * 2.0 / 3.0
ret += (20.0 * sin(x * Math.PI) + 40.0 * sin(x / 3.0 * Math.PI)) * 2.0 / 3.0
ret += (150.0 * sin(x / 12.0 * Math.PI) + 300.0 * sin(x / 30.0 * Math.PI)) * 2.0 / 3.0
return ret
}
transformLat and transformLon are the polynomials and sine functions mentioned above, where x and y are the longitude and latitude differences relative to 105°E and 35°N. The resulting offset is in meters: the latitude direction is divided by the meridian radius of curvature, and the longitude direction by the radius of the circle of latitude (the prime vertical radius of curvature times cos(latitude)), and then converted to degrees, which is what the magic lines in the code do.
Also, the Notes section of AMap’s developer guide mentions that the location SDK returns AMap coordinates (GCJ-02) in China and GPS coordinates (WGS84) overseas, so before converting I also need to check whether the position is in China, and if the position I get is outside China, there’s no need to convert it. The code circulating online usually checks this with latitude and longitude, treating anything within a specified range as being in China. But this is only a rough check against a rectangular boundary, and it actually takes in quite a few places outside China as well, such as the Korean Peninsula, western Japan, Mongolia, Southeast Asia and India. In those places AMap already returns WGS84, so converting again would actually make the position wrong.
Looking at the SDK, I should use the getCoordType() method to decide. It returns the coordinate system of this location result. According to the SDK reference manual , there are two possible return values: COORD_TYPE_GCJ02 and COORD_TYPE_WGS84. Whether the position is in China or abroad, and which coordinate system to provide, is decided and handled by the AMap SDK itself, so I only need to do the conversion when the SDK returns COORD_TYPE_GCJ02:
if (location.coordType == AMapLocation.COORD_TYPE_GCJ02) {
val wgs = convert(location.latitude, location.longitude)
location.latitude = wgs[0]
location.longitude = wgs[1]
}
Optimizing the Reported Accuracy Field
I also changed one thing in the original code. When the original Traccar client builds a position to report, it overwrites the accuracy with 0 if the provider is gps, and only reports the real accuracy for network positions. But GPS positions actually have an accuracy too, and the AMap SDK’s GPS positions also have the provider gps (network positions are lbs), so the accuracy of GPS positions would also be overwritten with 0. So I changed it to report the real accuracy for every kind of position:
// Original
accuracy = if (location.provider != null && location.provider != LocationManager.GPS_PROVIDER) {
location.accuracy.toDouble()
} else {
0.0
},
// Simply changed to
accuracy = location.accuracy.toDouble(),
Real-World Results
I compiled it and tested it on my phone. It gets positions reliably indoors too, the accuracy is much better than native network positioning, and after converting back to WGS84 the positions match the actual locations.
May 2026 Update
The AMap location SDK was updated to version 11.1.000 in 2026, so I upgraded the dependency to this version as well, and it works fine after recompiling.