BD09坐标(百度坐标) WGS84(GPS坐标) GCJ02(国测局坐标) 的相互转换

谷歌地图采用的是WGS84地理坐标系(中国范围除外),谷歌中国地图、搜搜中国地图、高德地图采用的是GCJ02地理坐标系,百度采用的是BD09坐标系,而设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,所以我们要根据得到的经纬度的坐标类型和地图厂商类型在地图上标点,否则会出现获取的位置误差

为什么不统一用WGS84地理坐标系这就是国家地理测绘总局对于出版地图的要求,出版地图必须符合GCJ02坐标系标准了,也就是国家规定不能直接使用WGS84地理坐标系。所以定位大家感觉不准确很多又叫出版地图为火星地图其实只是坐标系不一样而已。

    以百度地图为例,百度地图采用的是自己的BD09坐标。如果我们通过GPS获取的坐标,也就是WGS84坐标,那么我们首先要将WGS坐标转换成BD09坐标。代码如下:

package com.jiuqi.mobile.patrol.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.Socket;

import java.text.ParseException;

/**

 * 将真实的GPS经纬度信息转换成百度地图的经纬度

 * */

public class GpsToBaidu {

public static void main(String args[]) throws ParseException {

String xy = changgeXY("113.684405", "34.785423");

System.out.println("——"+xy);

}

/**

 * 转换经纬度

 * */

public static String changgeXY(String xx, String yy) {

try {

Socket s = new Socket("api.map.baidu.com", 80);

BufferedReader br = new BufferedReader(new InputStreamReader(s

.getInputStream(), "UTF-8"));

OutputStream out = s.getOutputStream();

StringBuffer sb = new StringBuffer(

"GET /ag/coord/convert?from=0&to=4");

sb.append("&x=" + xx + "&y=" + yy);

sb.append("&callback=BMap.Convertor.cbk_3976 HTTP/1.1 ");

sb.append("User-Agent: Java/1.6.0_20 ");

sb.append("Host: api.map.baidu.com:80 ");

sb

.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 ");

sb.append("Connection: Close ");

sb.append(" ");

out.write(sb.toString().getBytes());

String json = "";

String tmp = "";

while ((tmp = br.readLine()) != null) {

// System.out.println(tmp);

json += tmp;

}

            System.out.println(json);

int start = json.indexOf("cbk_3976");

int end = json.lastIndexOf("}");

if (start != -1 && end != -1 && json.contains(""x":"")) {

json = json.substring(start, end);

String[] point = json.split(",");

String x = point[1].split(":")[1].replace(""", "");

String y = point[2].split(":")[1].replace(""", "");

return (new String(decode(x)) + "," + new String(decode(y)));

} else {

System.out.println("gps坐标无效!!");

}

out.close();

br.close();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

 * 解码

 * 

 * @param str

 * @return string

 */

public static byte[] decode(String str) {

byte[] bt = null;

try {

BASE64Decoder decoder = new BASE64Decoder();

bt = decoder.decodeBuffer(str);

// System.out.println(new String (bt));

} catch (IOException e) {

e.printStackTrace();

}

return bt;

}

}


 如果拿到的坐标是国测局坐标,也就是  GCJ02,那么使用下面方法,可以转换成BD09

/**

*  国测局坐标和百度坐标的相互转换

*/

public class GCJToBD {

public static void main(String[] args) {

double l=113.69147;

double a=34.78425;

double lng=bd_encrypt(l, a)[0];

double lat=bd_encrypt(l, a)[1];

System.out.println(lng);

System.out.println(lat);

}

static double x_pi = 3.14159265358979324 * 3000.0 / 180.0; 

/**

 * 火星坐标系 (国测局标准)(GCJ-02) 转换为百度坐标系 (BD-09) 的转换算法

 * @param gg_lon

 * @param gg_lat

 * @return

 */

public static double[] bd_encrypt(double gg_lon,double gg_lat){ 

double[] d=new double[2];

    double x = gg_lon, y = gg_lat;  

    double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);  

    double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);  

    double bd_lon = z * Math.cos(theta) + 0.0065;  

    double bd_lat = z * Math.sin(theta) + 0.006; 

    d[0]=bd_lon;

    d[1]=bd_lat;

    

    return d;

}  

/**

 *  BD-09 坐标转换成GCJ-02 坐标

 * @param bd_lon  经度

 * @param bd_lat  纬度

 * @return

 */

public static double[] bd_decrypt(double bd_lon,double bd_lat)  

  

{  

double[] d=new double[2];

    double x = bd_lon – 0.0065, y = bd_lat – 0.006;  

  

    double z = Math.sqrt(x * x + y * y) – 0.00002 * Math.sin(y * x_pi);  

  

    double theta = Math.atan2(y, x) – 0.000003 * Math.cos(x * x_pi);  

  

    double gg_lon = z * Math.cos(theta);  

  

    double gg_lat = z * Math.sin(theta);  

    d[0]=gg_lon;

    d[1]=gg_lat;

    return d;

}  

}

转换成百度坐标下的经纬度即可有在地图上标点,当然,百度获取的文字详情不如高德,有时想要获取位置详情,需要调用高德的接口,而高德采用的标准是 GCJ02,因此,如果拿到百度地图的经纬度或是GPS经纬度,则要转成国测局的标准,以获取位置详细信息,方法如下:

//WGS84(GPS) 转换成 GCJ02(国测局)

public class GPSToGCJ {

static double pi = 3.14159265358979324;  

static  double a = 6378245.0;  

static   double ee = 0.00669342162296594323;  

    // World Geodetic System ==> Mars Geodetic System  

    public static double[] transform(double wgLon,double wgLat) { 

     double[] d=new double[2];

     double mgLat;double mgLon;

        if (outOfChina(wgLat, wgLon)){  

            mgLat = wgLat;  

            mgLon = wgLon;  

        }  

        double dLat = transformLat(wgLon – 105.0, wgLat – 35.0);  

        double dLon = transformLon(wgLon – 105.0, wgLat – 35.0);  

        double radLat = wgLat / 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);  

        mgLat = wgLat + dLat;  

        mgLon = wgLon + dLon;  

        d[0]=mgLon;

        d[1]=mgLat;

        return d;

    }  

    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;  

    }  

    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;  

    }  

    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;  

    }  

    

    public static void main(String[] args) {

     double l=113.68444;

double a=34.78545;

double lng=transform(l, a)[0];

double lat=transform(l, a)[1];

System.out.println(lng);

System.out.println(lat);

}

}

如果配合android端返回经纬度,百度的api中类,可以设置返回的坐标

请注明出处,此文档来自“善思善学”。
原文地址:https://www.cnblogs.com/gtgl/p/5015692.html