步驟比較簡單
先上百度地圖API官網,申請一個應用AK(訪問憑據);查看一下高進度定位的API,看看是否都符合要求
下面直接上代碼
/**
* 根據ip獲取地理坐標
* @param ip
* @return
*/
public JSONObject getCoorsByIp(String ip){
if (null == ip) {
ip = "";
}
try {
URL url = new URL("http://api.map.baidu.com/highacciploc/v1?qcip="+ip+
"&qterm=pc&ak="+*********+"&coord=bd09ll");
InputStream inputStream = url.openStream();
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputReader);
String results=reader.readLine();
if(!StringUtils.hasText(results)){
return null;
}
JSONObject resultsJson = JSONObject.fromObject(results); //返回值為標准json格式
JSONObject resultJson = JSONObject.fromObject(resultsJson.get("result"));
String result = resultJson.get("error").toString();
if(!result.equals("161")){
logger.info("根據ip獲取經緯度失敗!");
return null;
}
JSONObject contentJson = JSONObject.fromObject(resultsJson.get("content"));
JSONObject coorJson = JSONObject.fromObject(contentJson.get("location"));
return coorJson;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
個人感覺百度的高進度定位還是不太准確,只能是定位大致區域
還有一種是普通定位,無非請求的url不一致,其他的都差不多