标签:
如题,用手机百度获取到的gps相关坐标信息与真实坐标信息有一定的偏差存在,至于为啥偏差,暂且深究。
下面事我搜索到的将误差减小到最小化的方法,仅供参考。
baidu经纬度坐标与google经纬度坐标都是经过转换的。使用下面那个url可以将原始坐标或者谷歌的坐标转换成baidu的坐标
http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=114.324821&y=22.605812,将得到的base64字符,还原就是真实的经纬度。
请求接口中有四个参数:
其中,from和to对应的值分别是:0真实坐标;2google坐标;4baidu坐标。
from:被转换的坐标体系
to:转换到这个坐标体系
x:经度
y:纬度
接口应答中有三个key,
应答格式如:{"error":0,"x":"MTE2LjMyMTYyMzg4MjIz","y":"NDAuMDY2NDE2Mjc4NzMx"}
其中x和y就是所需要的值
应用程序如下:
package com.baiduMap纠错; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import com.google.gson.Gson; import com.sun.xml.internal.messaging.saaj.util.Base64; class BaiduMapModifyValue{ String error; String x; String y; public String getError() { return error; } public void setError(String error) { this.error = error; } public String getX() { return x; } public void setX(String x) { this.x = x; } public String getY() { return y; } public void setY(String y) { this.y = y; } } public class BMapGISModify { public static String ConvertToString(InputStream inputStream){ InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder result = new StringBuilder(); String line = null; try { while((line = bufferedReader.readLine()) != null){ result.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try{ inputStreamReader.close(); inputStream.close(); bufferedReader.close(); }catch(IOException e){ e.printStackTrace(); } } return result.toString(); } /* * 入参 gis信息, "x,y" xy分别表示经纬度 * 出参gis纠正信息,内容 "x,y" xy分别表示经纬度 * */ public static String modifyGIS(String valueXY){ System.out.println("原始gis值:"+valueXY); String[] gisXY = valueXY.split(","); String gisX = gisXY[0]; String gisY = gisXY[1]; /* 修正连接地址 * from和to对应的值分别是:0真实坐标;2google坐标;4baidu坐标。 from:被转换的坐标体系 to:转换到这个坐标体系 x:经度 y:纬度*/ String urlValue = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x="+gisX+"&y="+gisY; HttpURLConnection urlConnection = null; //json解析回传值 String responseStr = ""; try{ URL url = new URL(urlValue); urlConnection = (HttpURLConnection)url.openConnection(); //GET Request Define: //urlConnection.setRequestMethod("GET"); urlConnection.connect(); //Connection Response From Test Servlet //System.out.println("Connection Response From Test Servlet"); InputStream inputStream = urlConnection.getInputStream(); //Convert Stream to String responseStr = ConvertToString(inputStream); //System.out.println(responseStr); }catch(IOException e){ System.out.println("百度gis纠正,url强求解析,出现异常"); }finally{ urlConnection.disconnect(); } BaiduMapModifyValue result = new Gson().fromJson(responseStr, BaiduMapModifyValue.class); if(!result.error .equals("0")){ System.out.println("百度地图纠正gis,返回结果标记位非0,异常"); return "resultError"; }else{ String gisXY_OK = Base64.base64Decode(result.getX())+","+Base64.base64Decode(result.getY()); System.out.println("纠正gis值:"+gisXY_OK); return gisXY_OK; } } public static void main(String[] args) { /* 百度地图拾取坐标系统网址连接 * http://api.map.baidu.com/lbsapi/getpoint/index.html?qq-pf-to=pcqq.group * 勾选‘坐标反查’,可以进行数值坐标反查地理坐标 * */ String gisInfo = "133.94404224820853,21.537722890239518"; modifyGIS(gisInfo); } }
标签:
原文地址:http://www.cnblogs.com/liule1225/p/4584101.html