import java.util.HashMap;
import java.util.Map;
/**
* 国标GPS转高德坐标系方法
* @Author: anshen
* @Date: 2022/6/28 17:54
*/
public class GPSCoordinateUtil {
private static final double PI = 3.14159265358979324;
private static final double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
//GPS---高德
public static Map<String,Double> gcj_encrypt(double lat, double lng){
//判断是否是国内坐标,如果是就计算,如果不是就原样返回
if (outOfChina(lat, lng)){
Map<String,Double> map = new HashMap<>();
map.put("lat",lat);
map.put("lng",lng);
return map;
}
Map<String, Double> delta = delta(lat, lng);
Map<String,Double> map = new HashMap<>();
Double lat1 = delta.get("lat");
lat1 = lat - 0 + lat1;
Double lng1 = delta.get("lng");
lng1 = lng - 0 + lng1;
map.put("lat",lat1);
map.put("lng",lng1);
return map;
}
public static Map<String,Double> delta(double lat, double lng) {
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
double a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
double ee = 0.00669342162296594323; // ee: 椭球的偏心率。
double dLat = transformLat(lng - 105.0, lat - 35.0);//lng是x,lat是y
double dLon = transformLng(lng - 105.0, lat - 35.0);//lng是x,lat是y
double radLat = lat / 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);
Map<String,Double> map = new HashMap<>();
map.put("lat",dLat);
map.put("lng",dLon);
return map;
}
//判断是否是国内坐标
public static boolean outOfChina(double lat, double lng){
if (lng - 72.004 < 0 || lng - 137.8347 > 0){
return true;
}
if (lat - 0.8293 < 0 || lat - 55.8271 > 0){
return true;
}
return false;
}
//lng是x,lat是y
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;
}
//lng是x,lat是y
public static double transformLng(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;
}
}
国标GPS坐标转高德坐标java
于 2022-06-28 18:34:22 首次发布