根据百度地图经纬度获取位置信息

/**
 * 根据百度地图经纬度获取位置信息  
**/
public class BaiduMapUtils {

    // 百度地图秘钥
    static String ak = "此处添加你的百度地图秘钥";


    /**
     * 根据经纬度获取位置信息  getLocationInfo
     *
     * @param longitude: 经度
     * @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);
        }
    }
 }

结果:
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值