原始 经纬坐标 生成 百度经纬坐标

实现 将 原始的经纬坐标变成百度经纬坐标。其实中里面就是调用了百度的api:<pre name="code" class="java">"http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="+/*经度*/xgps+"&y="+/*纬度*/ygps;
</pre><pre name="code" class="java">/**
		 * from: 来源坐标系   (0表示原始GPS坐标,2表示Google坐标)
		    to: 转换后的坐标   (4就是百度自己啦,好像这个必须是4才行)
		     x: 经度
           y: 纬度
		 */
就这么简单


 
package gps2bgps;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Decoder;

public class ToBGps {
	
	/**
	 * 接受两个参数,xgps用来接受原始经度,ygps用来接受原始纬度
	 * @param xgps
	 * @param ygps
	 */
	public static void getBGps(String xgps,String ygps){
		String baiduUrl = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="+/*经度*/xgps+"&y="+/*纬度*/ygps;
		//目前两个URL来或去百度的坐标。但不知道哪一个是正确的
		System.out.println(baiduUrl);
		/**
		 * from: 来源坐标系   (0表示原始GPS坐标,2表示Google坐标)
		    to: 转换后的坐标   (4就是百度自己啦,好像这个必须是4才行)
		     x: 经度
           y: 纬度
		 */
		//"http://api.map.baidu.com/ag/coord/convert?x="+xgps+"&y="+ygps+"&from=0&to=2&mode=1";
		//http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=113.01930956&y=22.8370871
		Map<String,String> map = null;
		try {
			java.net.URI uri = new URI(baiduUrl);
			URL url = uri.toURL();
			InputStream in = url.openStream();
			byte[] b = new byte[in.available()];
			in.read(b);
			String result = new String(b);
			System.out.println("------->"+result);
			//"error":0,"x":"MTEzLjAzMTgzMzQ5NDU5","y":"MjIuODQ5NTQ4NDI3OTQ1"
			String status = result.substring(result.indexOf("\"error\":")+8,result.indexOf("\"error\":")+9);
			String xBaseStr =  result.substring(result.indexOf("\"x\":\"")+5,result.indexOf(",\"y\":\"")-1);
			String yBaseStr = result.substring(result.indexOf(",\"y\":\"")+6,result.indexOf("\"}"));
			System.out.println("x-->"+xBaseStr);
			System.out.println("y-->"+yBaseStr);
			System.out.println(status);
			if("0".equals(status)){
				map = DecoredBase64(xBaseStr,yBaseStr);
			}
			in.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		System.out.println(map);
	}
	
	public static Map<String,String> DecoredBase64(String x,String y){
		byte[] xb = null;
		byte[] yb = null;
		try {
			xb = new BASE64Decoder().decodeBuffer(x);
			yb = new BASE64Decoder().decodeBuffer(y);
		} catch (IOException e) {
			System.err.println("转码出错");
		}
		Map<String,String> bgps = new HashMap<String,String>();
		bgps.put("xbgps", new String(xb));
		bgps.put("ybgps", new String(yb));
		return bgps;
	}
	
	@org.junit.Test
	public void test() throws IOException{
		//Map m = ToBGps.DecoredBase64("MTEzLjAzMTEwNDAzMzEx", "MjIuODQwNTg1NDc4MjYz");
		//System.out.print(m);
		getBGps("113.02004184","22.84603575");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值