开发地图应用服务时,一定会接触到各种坐标系,而保证坐标系的正确与合理是一切数据分析的前提,总的来说,坐标系可以分为两大类:地理坐标系GCS(Geographic Coordinate System)和投影坐标系PCS(Projected Coordinate System),地理坐标系进行地图投影后就变成了投影坐标系。例如我们常见的世界地图,使用的就是一种墨卡托投影。
其中地理坐标系又可分为参心坐标系和地心坐标系,常见的参心坐标系北京54、西安80,常见的地心坐标系有WGS84、GCJ02、BD09、GCS2000。
WGS84 是为 GPS 全球定位系统建立的坐标系统,是世界上第一个统一的地心坐标系,因此也被称为大地坐标系、原始坐标系。一般通过 GPS 记录仪记录下来的经纬度,就是基于 WGS84 坐标系的数据。Google 和高德地图定位的的经纬度(国外)都是基于WGS84坐标系的;但是在国内是不允许直接用 WGS84 坐标系标注的,必须经过加密后才能使用。
1.2 GCJ02(国家测量局02号标准)GCJ02 是由中国国家测绘局制订的地理信息系统的坐标系统,是在 WGS84 经纬度的基础上执行加密算法而成。因为 GPS 得到的经纬度直接在 GCJ02 坐标系下会定位到错误的地点,有种到了火星的感觉,因此在坊间也将 GCJ-02 戏称为火星坐标系。
国测局规定,国内出版的各种地图系统(包括电子形式),必须至少采用 GCJ02 对地理位置进行首次加密的坐标系,腾讯(搜搜)地图、阿里云地图、Google和高德(国内)都是使用 GCJ02 坐标系,可以说 GCJ02 是国内最广泛使用的坐标系。
1.3 BD09(百度坐标系09)BD09 是在 GCJ02 坐标系的基础上再次加密偏移后形成的坐标系,官方解释为进一步保护用户隐私,当前只适用于百度地图。
GPS经纬度是39°54'26.2"N 116°23'28.4"E,转化为度的单位就是39.907270 116.391213(小数部分 = 分 / 60 + 秒 / 3600)
2. 坐标系转换- 国测局规定:互联网地图在国内必须至少使用 GCJ02 进行首次加密,不允许直接使用 WGS84 坐标下的地理数据,同时任何坐标系均不可转换为 WGS84 坐标。因此不存在将 GCJ02 坐标转换为 WGS84 坐标的官方转换方法。
- 目前百度 API 提供了从其它坐标系转换为 BD09 坐标系的 API,但却没有从 BD09 坐标系转为其他坐标系的API。
WGS84转换测试工具 可将 WGS84 转换成 GCJ02、BD09ll 坐标系:
2.2 高德坐标系转换高德坐标系转换工具 能够将用户输入的非高德坐标(GPS坐标、mapbar坐标、baidu坐标)转换成高德坐标:
以上的两个工具可以用来验证Java坐标系转换结果是否正确。
package yz.com.javautil.utilpackage;
import yz.com.javautil.vo.TransformPoint;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class CoordinateTransferUtils {
private static final double MIN_LON = 72.004D;
private static final double MAX_LON = 137.8347D;
private static final double MIN_LAT = 0.8293D;
private static final double MAX_LAT = 55.8271D;
private static final double PI = 3.14159265358979324D;
private static final double A = 6378245.0D;
private static final double EE = 0.00669342162296594323D;
public static void wgs84ToGcj02(T t) {
wgs84ToGcj02(t::getLng, t::getLat, t::setTransformLng, t::setTransformLat);
}
public static Map wgs84ToGcj02(double fromLon, double fromLat) {
HashMap transformRes = new HashMap<>(2);
// 国外坐标不用进行加密
if (outOfChina(fromLon, fromLat)) {
transformRes.put("lon", fromLon);
transformRes.put("lat", fromLat);
return transformRes;
}
// 计算转换后的经纬度坐标
double dLat = transformLat(fromLon - 105.0, fromLat - 35.0);
double dLon = transformLon(fromLon - 105.0, fromLat - 35.0);
double radLat = fromLat / 180.0 * PI;
double magic = Math.sin(radLat);
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 = fromLat + dLat;
double mgLon = fromLon + dLon;
transformRes.put("lon", new BigDecimal(mgLon + "", new MathContext(9, RoundingMode.HALF_UP)).doublevalue());
transformRes.put("lat", new BigDecimal(mgLat + "", new MathContext(9, RoundingMode.HALF_UP)).doublevalue());
return transformRes;
}
public static Map gcj02ToWgs84(double lon, double lat) {
Map transformRes = new HashMap<>(2);
double longitude = lon * 2 - wgs84ToGcj02(lon, lat).get("lon");
double latitude = lat * 2 - wgs84ToGcj02(lon, lat).get("lat");
transformRes.put("lon", longitude);
transformRes.put("lat", latitude);
return transformRes;
}
public static Map Gcj02ToBd09(double gcjLat, double gcjLng) {
Map transformRes = new HashMap<>(2);
double z = Math.sqrt(gcjLng * gcjLng + gcjLat * gcjLat) + 0.00002 * Math.sin(gcjLat * PI);
double theta = Math.atan2(gcjLat, gcjLng) + 0.000003 * Math.cos(gcjLng * PI);
transformRes.put("lon", z * Math.cos(theta) + 0.0065);
transformRes.put("lat", z * Math.sin(theta) + 0.006);
return transformRes;
}
public static Map bd09ToGcj02(double bdLat, double bdLng) {
Map transformRes = new HashMap<>(2);
double x = bdLng - 0.0065, y = bdLat - 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);
transformRes.put("lon", z * Math.cos(theta));
transformRes.put("lat", z * Math.sin(theta));
return transformRes;
}
private static void wgs84ToGcj02(Supplier fromLon, Supplier fromLat, Consumer toLon, Consumer toLat) {
wgs84ToGcj02(Optional.ofNullable(fromLon.get()).orElse(BigDecimal.ZERO), Optional.ofNullable(fromLat.get()).orElse(BigDecimal.ZERO), toLon, toLat);
}
private static void wgs84ToGcj02(BigDecimal fromLon, BigDecimal fromLat, Consumer toLon, Consumer toLat) {
final Map transformRes = wgs84ToGcj02(fromLon.doublevalue(), fromLat.doublevalue());
toLon.accept(new BigDecimal(transformRes.get("lon") + ""));
toLat.accept(new BigDecimal(transformRes.get("lat") + ""));
}
private static boolean outOfChina(double lon, double lat) {
if (lon < MIN_LON || lon > MAX_LON) {
return true;
}
return lat < MIN_LAT || lat > MAX_LAT;
}
private static double transformLat(double x, double y) {
double transform = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
transform += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
transform += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
return transform;
}
private static double transformLon(double x, double y) {
double transform = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
transform += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
transform += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
transform += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
return transform;
}
public static void main(String[] args) {
// 转换后的 113.733069118924,34.409617513021
double lon = 113.726919D;
double lat = 34.410907D;
Map transformResult = wgs84ToGcj02(lon, lat);
System.out.println("WGS84ToGCJ02 转换前经纬度(" + lon + "," + lat + ") 转换后经纬度(" + transformResult.get("lon") + "," + transformResult.get("lat") + ")");
double lon2 = 113.733069118924D;
double lat2 = 34.4096175D;
Map transform2Result = gcj02ToWgs84(lon2, lat2);
System.out.println("GCJ02ToWGS84 转换前经纬度(" + lon2 + "," + lat2 + ") 转换后经纬度(" + transform2Result.get("lon") + "," + transform2Result.get("lat") + ")");
}
}



