经由过程百度地图API,将百度坐标转换成GPS经纬度

本文介绍了一种在没有官方转换算法的情况下,实现GPS坐标与百度地图坐标相互转换的方法。通过巧妙利用百度地图API,实现了高精度的坐标转换,误差在20米以内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

百度地图API中,有GPS坐标转百度坐标的功能
http://dev.baidu.com/wiki/static/map/API/examples/?v=1.2&0_6#0&6

http接口是:http://api.map.baidu.com/ag/coord/convert?=0&to=4&x=116.397428&y=39.90923&callback=BMap.Convertor.cbk_7594
返回成果坐标是经由过程base64加密的。
这个转换算法百度是不会公开的,并且百度也没有供给百度坐标转成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

在http://dev.baidu.com/wiki/static/map/API/examples/?v=1.2&0_6#0&6 将此坐标输入GPS数据项中获得的成果是:116.39743826208,39.909194650838

代码实现:

 

package com.baidu.api;

import it.sauronsoftware.base64.Base64;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

//import javabase.gps2baidu.JSONObject;

public class Gps2BaiDu {
 public static void main(String[] args) {
  // 转换前的GPS坐标
  double x = 116.31303919878;
  double y = 39.983187845538;

  // 获得gps坐标
  double[] gps = baidu2Gps(x, y);
  System.out.println("gps坐标" + gps[0] + " " + gps[1]);
  // gps坐标 转换 百度坐标
  double[] baidu = gps2Baidu(gps[0],gps[1]);
  System.out.println("百度地图:" + baidu[0] + " " + baidu[1]);

 }

 /**
  * baidu坐标 转 gps坐标
  *
  * @param x
  * @param y
  * @return
  */
 static double[] baidu2Gps(double x, double y) {
  double[] baidu2Gps = gps2Baidu(x, y);
  double x1 = baidu2Gps[0];
  double y1 = baidu2Gps[1];

  double gps_x = 2 * x - x1;
  double gps_y = 2 * y - y1;

  baidu2Gps[0] = gps_x;
  baidu2Gps[1] = gps_y;
  return baidu2Gps;
 }

 // gps 坐标转换成 百度坐标
 static double[] gps2Baidu(double x, double y) {
  double[] zuobiao = new double[2];
  // gps坐标的type=0
  // google坐标的type=2
  // baidu坐标的type=4
  String path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="
    + x + "&y=" + y + "&callback=BMap.Convertor.cbk_7594 ";
  try {
   // 使用http请求获取转换结果
   URL url = new URL(path);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("GET");
   conn.setConnectTimeout(5 * 1000);
   InputStream inStream = conn.getInputStream();

   ByteArrayOutputStream outStream = new ByteArrayOutputStream();
   byte[] buffer = new byte[1024];
   int len = 0;
   while ((len = inStream.read(buffer)) != -1) {
    outStream.write(buffer, 0, len);
   }
   // 得到返回的结果
   String res = outStream.toString();
   // 处理结果
   if (res.indexOf("(") > 0 && res.indexOf(")") > 0) {
    String str = res.substring(res.indexOf("(") + 1,
      res.indexOf(")"));
    String err = res.substring(res.indexOf("error") + 7,
      res.indexOf("error") + 8);
   }

   if (res.indexOf("(") > 0 && res.indexOf(")") > 0) {
    String str = res.substring(res.indexOf("(") + 1,
      res.indexOf(")"));
    String err = res.substring(res.indexOf("error") + 7,
      res.indexOf("error") + 8);
    if ("0".equals(err)) {
     JSONObject js = new JSONObject(str);
     // 编码转换
     String x1 = new String(Base64.decode(js.getString("x")));
     String y1 = new String(Base64.decode(js.getString("y")));
     zuobiao[0] = Double.parseDouble(x1);
     zuobiao[1] = Double.parseDouble(y1);
    }
   }
   return zuobiao;

  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
}

/**

*   做完之后测试了一下,误差在20米以内,如果有更精确的算法,希望大家能多多交流

**/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值