2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 经由过程百度地图API 将百度坐标转换成GPS经纬度

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

时间:2019-06-09 21:07:54

相关推荐

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

百度地图API中,有GPS坐标转百度坐标的功能

/wiki/static/map/API/examples/?v=1.2&0_6#0&6

http接口是:http://api./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

在/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 .HttpURLConnection;

import .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./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米以内,如果有更精确的算法,希望大家能多多交流

**/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。