通过经纬度计算位置(百度地图)

package com.ys.modules.protocol.util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ys.manager.protocol.entity.SysCarRecord;
import org.springframework.web.util.UriUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.Map;

public class baiduMapUtil extends SysCarRecord {

    public static String URL = "https://api.map.baidu.com/reverse_geocoding/v3?";

    public static String AK = "PSx9BXByDS2663AhmvqBOkzK12ZlQbsv";

    public static void main(String[] args) throws Exception {

        baiduMapUtil snCal = new baiduMapUtil();

        Map params = new LinkedHashMap<String, String>();
        params.put("ak", AK);
        params.put("output", "json");
        params.put("coordtype", "wgs84ll");
        params.put("extensions_poi", "0");
        params.put("location", "29.225696563611,111.49884033194");


        snCal.requestGetAK(URL, params);
    }

    /**
     * 默认ak
     * 选择了ak,使用IP白名单校验:
     * 根据您选择的AK已为您生成调用代码
     * 检测到您当前的ak设置了IP白名单校验
     * 您的IP白名单中的IP非公网IP,请设置为公网IP,否则将请求失败
     * 请在IP地址为0.0.0.0/0 外网IP的计算发起请求,否则将请求失败
     * @return
     */
    public static String requestGetAK(String strUrl, Map<String, String> param) throws Exception {
        if (strUrl == null || strUrl.length() <= 0 || param == null || param.size() <= 0) {
            return strUrl;
        }

        StringBuffer queryString = new StringBuffer();
        queryString.append(strUrl);
        for (Map.Entry<?, ?> pair : param.entrySet()) {
            queryString.append(pair.getKey() + "=");
            //    第一种方式使用的 jdk 自带的转码方式  第二种方式使用的 spring 的转码方法 两种均可
            //    queryString.append(URLEncoder.encode((String) pair.getValue(), "UTF-8").replace("+", "%20") + "&");
            queryString.append(UriUtils.encode((String) pair.getValue(), "UTF-8") + "&");
        }

        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }

        java.net.URL url = new URL(queryString.toString());
        System.out.println(queryString.toString());
        URLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.connect();

        InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        isr.close();
        String formattedAddress = parseFormattedAddress(buffer.toString());
        //System.out.println("formatted_address: " + formattedAddress);
        return formattedAddress;

    }
    public static String parseFormattedAddress(String jsonResponse) {
        // 解析JSON响应,获取formatted_address字段的值
        // 这里可以使用JSON库,如Jackson、Gson等来解析JSON
        // 示例代码仅供参考,具体实现需要根据实际情况进行调整
        // 示例代码中使用了Jackson库来解析JSON
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(jsonResponse);
            JsonNode resultNode = rootNode.get("result");
            if (resultNode != null) {
                JsonNode formattedAddressNode = resultNode.get("formatted_address");
                if (formattedAddressNode != null) {
                    return formattedAddressNode.asText();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值