百度经纬度和google经纬度互转

1、百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化

private const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
/// <summary>
/// 中国正常坐标系GCJ02协议的坐标,转到 百度地图对应的 BD09 协议坐标
/// </summary>
/// <param name="lat">维度</param>
/// <param name="lng">经度</param>
public static void Convert_GCJ02_To_BD09(ref double lat,ref double lng)
{
double x = lng, y = 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);
lng = z * Math.Cos(theta) + 0.0065;
lat = z * Math.Sin(theta) + 0.006;
}
/// <summary>
/// 百度地图对应的 BD09 协议坐标,转到 中国正常坐标系GCJ02协议的坐标
/// </summary>
/// <param name="lat">维度</param>
/// <param name="lng">经度</param>
public static void Convert_BD09_To_GCJ02(ref double lat, ref double lng)
{
double x = lng - 0.0065, y = 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);
lng = z * Math.Cos(theta);
lat = z * Math.Sin(theta);
}

目的:将百度地图经纬度 互相转换到 腾讯地图/Google 对应的经纬度.

方法1:使用代码进行转换<存在的问题:转换之后误差大,基本不可用>
public  static  void  Convert_BD09_To_GCJ02( double  lat,  double  lng) {
             double  x_pi = Math.  PI  * 3000.0 / 180.0;
             double  x = lng - 0.0065, y = 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);
           lng = z * Math. cos(theta );
           lat = z * Math. sin(theta );
           
           System.  out .println(lng);
           System.  out .println(lat);
}

方法2:
该网站提供转换服务,坐标较为准确,可用,后台调用没有仔细研究

方法3:
使用腾讯地图,详见1.html

直接调用的地址<百度地图转为腾讯地图>,points=经纬度,传入百度的即可:
直接调用的地址<腾讯地图转为百度地图>,points=经纬度,传入百度的即可:
http://mapi.map.qq.com/translate/?type=3&points=116.396135,39.911082;116.396135,39.911082&output=jsonp
坐标查询地址:
  
http://www.gpsspg.com/maps.htm通过经纬度获取每种地图的坐标
protected void runTest() throws Throwable {

	try {
		BaiduLocation bl = new BaiduLocation();
			bl.gpsx = 120;//经度
			bl.gpsy = 30;//纬度
			GetBaiduLocation(bl);
			if(bl.ok) {
				int baidux = (int)(bl.baidux*1E6);
				int baiduy = (int)(bl.baiduy*1E6);
				// 转换成功,这个坐标是百度专用的
			}
			else {
				/// 转换失败
			}
	}
	catch(Exception ex) {
	}
}

class BaiduLocation {
	public float gpsx, gpsy;
	public float baidux, baiduy;
	public boolean ok = false;
}

public static String GetBaiduLocation(float x, float y) throws MalformedURLException, IOException {
	String url = String.format("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=%f&y=%f", x, y);
	HttpURLConnection urlConnection = (HttpURLConnection)(new URL(url).openConnection());
	urlConnection.connect();
	BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
	String lines = reader.readLine();
    reader.close(); 
    urlConnection.disconnect();
    return lines;
}	

public static boolean GetBaiduLocation(BaiduLocation bl) {
	try {
		bl.ok = false;
		String res = GetBaiduLocation(bl.gpsx, bl.gpsy);
		if(res.startsWith("{") && res.endsWith("}")) {
			res = res.substring(1, res.length() - 2).replace("\"", "");
			String[] lines = res.split(",");
			for(String line : lines) {
				String[] items = line.split(":");
				if(items.length == 2) {
					if("error".equals(items[0])) {
						bl.ok = "0".equals(items[1]);
					}
					if("x".equals(items[0])) {
						bl.baidux = ConvertBase64(items[1]);
					}
					if("y".equals(items[0])) {
						bl.baiduy = ConvertBase64(items[1]);
					}
				}
			}
		}
	} catch (Exception e) {
		bl.ok = false;
	} 
	return bl.ok;	
}
private static float ConvertBase64(String str) {
	byte[] bs = Base64.decode(str);		
	return Float.valueOf(new String(bs));
}


总结:
     由于 腾讯的经纬度和google经纬度是同一个坐标系,百度经纬度为自己的体系,目前网上搜索到的算法代码,都不是十分精确,所以建议使用地图开发方提供的Api进行转换.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值