关于GPS坐标转百度坐标与goolg坐标转百度坐标java代码实现方法

关于GPS坐标转百度坐标与goolg坐标转百度坐标java代码实现方法

百度显示坐标经过了两次加密所以需要转换。
<pre name="code" class="java">package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.ParseException;

import sun.misc.BASE64Decoder;

/**
 * GPS坐标转百度坐标测试
 * @author Administrator
 *
 */
public class LoctionTest {
	public static void main(String args[]) throws ParseException {
		changgeXY("120.11508", "30.279146");
	}

	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\r\n");
			sb.append("User-Agent: Java/1.6.0_20\r\n");
			sb.append("Host: api.map.baidu.com:80\r\n");
			sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
			sb.append("Connection: Close\r\n");
			sb.append("\r\n");
			out.write(sb.toString().getBytes());
			String json = "";
			String tmp = "";
			while ((tmp = br.readLine()) != null) {
				// System.out.println(tmp);
				json += tmp;
			}

			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("\"", "");
				//System.out.println(new String(decode(x)) + "," + new String(decode(y)));
				//String result = new String(decode(x)) + "," + new String(decode(y));
				//System.out.println(result.substring(0, result.indexOf(",")));
				//System.out.println(result.substring(result.indexOf(",")+1));
				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;
	}

}
//===========================================================================================================================================

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;

import net.sf.json.JSONObject;

import com.alibaba.fastjson.util.Base64;
/**
 * goolg坐标转百度坐标
 * @author Administrator
 *
 */
public class GoolgToBaiduTest {
public static void main(String args[]) throws ParseException {
		String result = GoolgToBaidu("120.11508", "30.279146");
		JSONObject  dataJson=JSONObject.fromObject(result);
		String x = String.valueOf(dataJson.get("x"));
		String y = String.valueOf(dataJson.get("y"));
		byte[] x1=Base64.decodeFast(x);
		byte[] y1=Base64.decodeFast(y);
		double lon = Double.parseDouble(new String(x1));
		double lat = Double.parseDouble(new String(y1));
		System.out.println(lon+","+lat);
}
public static  String GoolgToBaidu(String lon, String lat) {
String result = "";
    String url="http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x="+lon+"&y="+lat;
    try{
         //String urlName = url + "?"+param;
         URL U = new URL(url);
         URLConnection connection = U.openConnection();


         connection.connect(); 
  BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  String line;
  while ((line = in.readLine())!= null)
  {
   result += line;
  }
  
  in.close();   
 }catch(Exception e){
  System.out.println("没有结果!"+e);
 }
 return result;
}

百度转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 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值