---
title: "百度坐标与GPS坐标转换"
author: "xwangly"
date: "2014-08-06T15:37:27Z"
tags: []
categories: ["移动开发"]
slug: "gps"
draft: false
lastmod: "2014-08-06T15:37:27Z"
id: 2100982
url: "https://xwangly.iteye.com/blog/2100982"
---

官网这么说的

 

4.3 百度坐标为何有偏移？

```
国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上，进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度，需要通过坐标转换接口进行转换。
```

百度地图API中，有GPS坐标转百度坐标的功能   
http://developer.baidu.com/map/wiki/index.php?title=androidsdk/guide/tool

这个转换算法百度是不会公开的，而且百度也没有提供百度坐标转成GPS坐标功能，这里我用了取巧的办法。 

百度坐标和GPS坐标转换在很近的距离时偏差非常接近。   
假设你有百度坐标：x1=116.397428，y1=39.90923   
把这个坐标当成GPS坐标，通过接口获得他的百度坐标：x2=116.41004950566，y2=39.916979519873 

通过计算就可以得到GPS的坐标：   
x = 2\*x1-x2，y = 2\*y1-y2   
x=116.38480649434001   
y=39.901480480127 

convertBaiduToGPS befour:(30.464729,114.577475)

convertBaiduToGPS after:(30.461196,114.565995)

 

转换方法如下：

    public static LatLng convertGPSToBaidu(LatLng sourceLatLng) {

     // 将GPS设备采集的原始GPS坐标转换成百度坐标  

        CoordinateConverter converter  = new CoordinateConverter();  

        converter.from(CoordType.GPS);  

        // sourceLatLng待转换坐标  

        converter.coord(sourceLatLng);  

        LatLng desLatLng = converter.convert();

        return desLatLng;

    }

 

    public static LatLng convertBaiduToGPS(LatLng sourceLatLng) {

        // 将GPS设备采集的原始GPS坐标转换成百度坐标

        CoordinateConverter converter = new CoordinateConverter();

        converter.from(CoordType.GPS);

        // sourceLatLng待转换坐标

        converter.coord(sourceLatLng);

        LatLng desLatLng = converter.convert();

        double latitude = 2 * sourceLatLng.latitude - desLatLng.latitude;

        double longitude = 2 * sourceLatLng.longitude - desLatLng.longitude;

        BigDecimal bdLatitude = new BigDecimal(latitude);

        bdLatitude = bdLatitude.setScale(6, BigDecimal.ROUND\_HALF\_UP);

        BigDecimal bdLongitude = new BigDecimal(longitude);

        bdLongitude = bdLongitude.setScale(6, BigDecimal.ROUND\_HALF\_UP);

        return new LatLng(bdLatitude.doubleValue(), bdLongitude.doubleValue());

    }