根据百度地图经纬度获取位置信息
生活随笔
收集整理的這篇文章主要介紹了
根据百度地图经纬度获取位置信息
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*** 根據(jù)百度地圖經(jīng)緯度獲取位置信息
**/
public class BaiduMapUtils {// 百度地圖秘鑰static String ak = "此處添加你的百度地圖秘鑰";/*** 根據(jù)經(jīng)緯度獲取位置信息 getLocationInfo** @param longitude: 經(jīng)度* @param latitude: 緯度**/public static Map<String, Object> getLocationInfo(String longitude, String latitude) {String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype=wgs84ll&location=" + latitude + "," + longitude;StringBuilder res = new StringBuilder();BufferedReader in = null;try {URL url = new URL(urlString);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setDoOutput(true);conn.setRequestMethod("GET");in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));String line;while ((line = in.readLine()) != null) {res.append(line).append("\n");}} catch (Exception e) {throw new RuntimeException(e);} finally {if (in != null) {try {in.close();} catch (IOException e) {throw new RuntimeException(e);}}}return JSON.parseObject(res.toString(), Map.class);}//測試public static void main(String[] args) {Map<String, Object> map = getLocationInfo("116.472673", "40.108383");if (!ObjectUtils.isEmpty(map) && "0".equals(String.valueOf(map.get("status")))) {Map result = JSONObject.parseObject(String.valueOf(map.get("result")), Map.class);String formattedAddress = result.get("formatted_address").toString();String business = result.get("business").toString();System.out.println("地址:" + formattedAddress + "," + business);}}}
結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的根据百度地图经纬度获取位置信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis的hscan替换hgetall
- 下一篇: R语言环境搭建