1 百度官方对百度坐标为何有偏移的解释
国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。
2 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法
GCJ-02(火星坐标) 和 BD-09 (百度坐标)
算法代码如下,其中 bd_encrypt 将 GCJ-02 坐标转换成 BD-09 坐标, bd_decrypt 反之。
3.备注:
参考如下,但是博主的代码有些许地方不规范,有点点小问题。附上改正后的代码
参考:https://blog.csdn.net/w605283073/article/details/64906518
/** * <p> * 百度地图坐标和火星坐标转换 * 高德坐标系统:https://lbs.amap.com/console/show/picker; * 百度坐标系统:http://api.map.baidu.com/lbsapi/getpoint/index.html * </p> * * @author luohq * @date 2018/11/27 */ public class CoodinateCovertor { private static double xPi = 3.14159265358979324 * 3000.0 / 180.0; private CoodinateCovertor() { } /** * 将火星坐标转变成百度坐标 * * @param marsCoordinate 火星坐标(高德、腾讯地图坐标等) * @return 百度坐标 */ public static Coordinate marsToBaidu(Coordinate marsCoordinate) { double x = marsCoordinate.getLongitude(); double y = marsCoordinate.getLatitude(); double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * xPi); double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * xPi); return new Coordinate(dataDigit(6, z * Math.cos(theta) + 0.0065), dataDigit(6, z * Math.sin(theta) + 0.006)); } /** * 将百度坐标转变成火星坐标 * * @param baiduCoordinate 百度坐标(百度地图坐标) * @return 火星坐标(高德、腾讯地图等) */ public static Coordinate baiduToMars(Coordinate baiduCoordinate) { double x = baiduCoordinate.getLongitude() - 0.0065; double y = baiduCoordinate.getLatitude() - 0.006; double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi); double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi); return new Coordinate(dataDigit(6, z * Math.cos(theta)), dataDigit(6, z * Math.sin(theta))); } /** * 对double类型数据保留小数点后多少位 * * @param digit 位数 * @param input 输入 * @return 保留小数位后的数 */ public static double dataDigit(int digit, double input) { return BigDecimal.valueOf(input).setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue(); } ------------------------- @Data @NoArgsConstructor @AllArgsConstructor public class Coordinate { /** * 经度 */ private double longitude; /** * 纬度 */ private double latitude; } ------------------------- public class CoodinateCovertorTest {
@Test
public void marsToBaidu() {
// eg:高德地图的"黄渠公园"ip(116.56846,39.927558)
Coordinate lngLat_bd = new Coordinate(116.56846, 39.927558);
System.out.println("火星坐标转百度坐标:" + CoodinateCovertor.marsToBaidu(lngLat_bd));
}
}