百度地图根据经纬度得到地理位置

时间:2021-09-18 10:53:13
String url = "http://api.map.baidu.com/geocoder/v2/?ak=2uZgkxkTV4BCdGfAWN6m3cra&callback=renderReverse&location=39.983424,116.322987&output=json&pois=0";
URL getUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置
String result = reader.readLine();
JSONObject json = JSONObject.fromObject(result);
String address= json.getString("result");
json = JSONObject.fromObject(address);
address = json.getString("formatted_address");
System.out.println(address);