Keywords: Java

# Map coordinate conversion

Recently, when it comes to the transformation of map coordinates, I began to have an in-depth understanding. Different coordinate systems use different standards, resulting in great latitude and longitude deviation of the same place in different maps.

# 1, Coordinate system

## 1. Concept

Coordinate system: a system used for positioning. Just like the two-dimensional Cartesian coordinate system, the unique position of a point in the Cartesian coordinate system can be determined by using (x,y). The coordinate system mentioned here is much more complex than the Cartesian coordinate system, but its function is the same. It is mainly used for positioning, that is, accurately positioning a point on the surface.
Geographic coordinate system: WGS84 is a geographic coordinate system. Geographic coordinate is a simple geometric modeling of the earth. For example, the earth is regarded as a sphere or quasi sphere, and then the points on the surface are projected onto the sphere to form a geographic coordinate system. WGS84 defines how to abstract the earth into a sphere or a sphere like rule. Or simply put, WGS84 is a set of parameters used to establish a sphere or quasi sphere to approximate the earth.
Projection coordinate system: since the earth is a sphere, a certain area of the earth is generally projected on a plane, and the resulting coordinate system is called the projection coordinate system.

Note:
WGS84: geographic coordinate system, which is used by Google Earth and Google Map outside China. In addition, at present, almost all devices positioning spatial positions use this coordinate system, such as the GPS system of mobile phones.
GCJ-02: the projection coordinate system, which is commonly referred to as the Mars coordinate system, seems to be used by Google Map China, Gaode and Tencent. This is encrypted by China on the basis of WGS84. The purpose is obvious.
BD09: projection coordinate system, which is used by Baidu map and encrypted twice on the basis of GCJ-02

## 2. Coordinate system used by different maps

MapCoordinate system
Baidu map APIBaidu coordinates
Baidu map APIBaidu coordinates
Tencent search map APIMars coordinates
Sohu Sogou map APISogou coordinates
Alibaba cloud map APIMars coordinates
MapBar map APIGraph bar coordinates
Gaode MapABC map APIMars coordinates
Lingtu 51ditu map APIMars coordinates

## 3. Conversion

### 3.1 algorithm conversion

```public class GPS {
private double lat;
private double lon;

public GPS(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

public String toString() {
return "lat:" + lat + "," + "lon:" + lon;
}
}

```
```/**
* Coordinate conversion tool class
* GCJ02: Mars coordinate system, the coordinate system formulated by the State Bureau of Surveying and mapping of China, is classified by WGS84. Google Map China and search map use, Gaode
* BD09:Baidu coordinate, GCJ02 confidential coordinate system
* Sogou coordinate system, graph bar coordinates, etc. are estimated to be encrypted on the basis of GCJ02
*/
public class GPSConverterUtils {
public static final String BAIDU_LBS_TYPE = "bd09ll";
public static double pi = 3.1415926535897932384626;
public static double a = 6378245.0;
public static double ee = 0.00669342162296594323;

/**
* 84 to Mars coordinate system (gcj-02) world geodetic system = = > Mars geodetic system
* @param lat
* @param lon
*/
public static GPS gps84_To_Gcj02(double lat, double lon) {
if (outOfChina(lat, lon)) {
return null;
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new GPS(mgLat, mgLon);
}

/**
* * Mars coordinate system (GCJ-02) to 84 * * @param lon * @param lat * @return
*/
public static GPS gcj_To_Gps84(double lat, double lon) {
GPS gps = transform(lat, lon);
double lontitude = lon * 2 - gps.getLon();
double latitude = lat * 2 - gps.getLat();
return new GPS(latitude, lontitude);
}

/**
* The conversion algorithm between Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) converts GCJ-02 coordinates into BD-09 coordinates
*
* @param gg_lat
* @param gg_lon
*/
public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) {
double x = gg_lon, y = gg_lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
double bd_lon = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new GPS(bd_lat, bd_lon);
}

/**
* * Conversion algorithm between Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) * convert BD-09 coordinates into GCJ-02 coordinates * * @ param
* bd_lat * @param bd_lon * @return
*/
public static GPS bd09_To_Gcj02(double bd_lat, double bd_lon) {
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new GPS(gg_lat, gg_lon);
}

/**
* (BD-09)-->84
* @param bd_lat
* @param bd_lon
* @return
*/
public static GPS bd09_To_Gps84(double bd_lat, double bd_lon) {

GPS gcj02 = bd09_To_Gcj02(bd_lat, bd_lon);
GPS map84 = gcj_To_Gps84(gcj02.getLat(),
gcj02.getLon());
return map84;

}

/**
* is or not outOfChina
* @param lat
* @param lon
* @return
*/
public static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}

public static GPS transform(double lat, double lon) {
if (outOfChina(lat, lon)) {
return new GPS(lat, lon);
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new GPS(mgLat, mgLon);
}

public static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}

public static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
* Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
* pi)) * 2.0 / 3.0;
return ret;
}
}

```

### 3.2 API conversion

#### 3.2.1 gaude coordinate conversion Web API

Online API documentation: http://lbs.amap.com/api/webservice/guide/api/convert/
Applicable scenarios:
In order to use Gaode service, only non Gaode coordinates can be converted to Gaode coordinates
instructions:
Step 1: apply for the "Web Service API" Key;
The second step is to splice the HTTP request URL. The Key applied in the first step must be sent together as a required parameter;
The third step is to receive the data returned by the HTTP request (JSON or XML format) and parse the data.
Unless otherwise stated, the input parameters and output data codes of the interface are unified as UTF-8.
Use example:
http://restapi.amap.com/v3/assistant/coordinate/convert?locations=116.481499 , 39.990475 & coordsys = GPS & output = XML & key = < user's key >

#### 3.2.2 Baidu coordinate conversion Web API

Online api documentation: http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
Applicable scenarios:
Support multiple coordinate conversion

#### 3.2.3 GPS coordinate conversion Web API

Online API documentation: http://www.gpsspg.com/api/convert/latlng/
Applicable scenarios:
Support multiple coordinate conversion

### 3.3 SDK conversion

#### 3.3.1 Gaode sdk api conversion

Online API documentation: http://lbs.amap.com/api/android-sdk/guide/computing-equipment/coordinate-transformation/

#### 3.3.2 Baidu sdk api conversion

Online api documentation: http://lbsyun.baidu.com/index.php?title=androidsdk/guide/tool/coordinate

Posted by cocpg on Wed, 29 Sep 2021 16:25:23 -0700