public class GdMapUtils {
public static final String GD_URL = "https://restapi.amap.com/v3/geocode/geo?key=自己去开发者申请的keay&address=";
/**
* 根据地址获取经纬度信息
*/
public static Object getGeographyInfoByAddress(String address) {
String result = "";
try {
// 把字符串转换为URL请求地址
URL url = new URL(GD_URL + address);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 连接会话
connection.connect();
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
// 循环读取流
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
HashMap map = JSON.parseObject(sb.toString(), HashMap.class);
Object geocodes = map.get("geocodes");
return ((JSONObject) ((JSONArray) geocodes).get(0)).get("location");
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
return result;
}
public static void main(String[] args) {
System.out.println(getGeographyInfoByAddress("广东省深圳市宝安区塘头一号路创维创新谷C座8层"));
}