基于java的百度地图逆向解析(根据坐标返回地址)
package com.citcc4.traffic.mirror.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RestController;
import com.citcc4.traffic.common.util.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
*
* 项目名称:civilized-traffic-cloudmirror 类名称:LocationController 创建人: QinBo
* 创建时间:2017年8月17日 下午2:14:37 类描述:
*
* @version
*/
@RestController
public class LocationUtil {
/**
*
* 输入经纬度坐标返回地址 key lng(经度),lat(纬度)
*/
public static JsonNode getposition(String loca) {
String lng=loca.substring(0, loca.indexOf(","));
String lat=loca.substring(loca.indexOf(",")+1);
String location=lat+","+lng;
BufferedReader in = null;
URL tirc = null;
try {
tirc = new URL("http://api.map.baidu.com/geocoder/v2/ak=key&location="+location+"&output=json&pois=0");
System.out.println(tirc);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
URLConnection connection = tirc.openConnection();
connection.setDoOutput(true);
in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
String str = sb.toString();
// System.out.println(str);
ObjectMapper mapper = new ObjectMapper();
if (StringUtils.isNotEmpty(str)) {
JsonNode jsonNode = mapper.readTree(str);
jsonNode.findValue("status").toString();
JsonNode resultNode = jsonNode.findValue("result");
JsonNode locationNode = resultNode.findValue("formatted_address");
return locationNode;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
原文链接:https://blog.csdn.net/u012454676/article/details/77351318