java根据经纬度获取省份城市(百度API)

发布于:2025-02-10 ⋅ 阅读:(43) ⋅ 点赞:(0)

百度api地址:全球逆地理编码 rgc 反geo检索 | 百度地图API SDK

代码中换一下AK即可运行。

/**
  * 选择了ak或使用IP白名单校验:
  */

package com.ssm;

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.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

public class Baidu {

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

    public static String AK = "换成你的AK";

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

    	Baidu snCal = new Baidu();

        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", "37.406033,118.003929");


        snCal.requestGetAK(URL, params);
    }

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

        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();
        System.out.println("AK: " + buffer.toString());
    }
}

运行结果:

AK: {
    "status": 0,
    "result": {
        "location": {
            "lng": 118.01652343089951,
            "lat": 37.4124810095552
        },
        "formatted_address": "山东省滨州市滨城区黄河十二路",
        "edz": {
            "name": ""
        },
        "business": "渤海九路,渤海八路,渤海七路",
        "business_info": [{
            "name": "渤海九路",
            "location": {
                "lng": 118.01332446974364,
                "lat": 37.39034100409529
            },
            "adcode": 371602,
            "distance": 0,
            "direction": "内"
        }, {
            "name": "渤海八路",
            "location": {
                "lng": 118.02082603939637,
                "lat": 37.39180703888226
            },
            "adcode": 371602,
            "distance": 0,
            "direction": "内"
        }, {
            "name": "渤海七路",
            "location": {
                "lng": 118.02489060233735,
                "lat": 37.38921814510701
            },
            "adcode": 371602,
            "distance": 477,
            "direction": "北"
        }],
        "addressComponent": {
            "country": "中国",
            "country_code": 0,
            "country_code_iso": "CHN",
            "country_code_iso2": "CN",
            "province": "山东省",
            "city": "滨州市",
            "city_level": 2,
            "district": "滨城区",
            "town": "市东街道",
            "town_code": "371602004",
            "distance": "",
            "direction": "",
            "adcode": "371602",
            "street": "黄河十二路",
            "street_number": ""
        },
        "pois": [],
        "roads": [],
        "poiRegions": [],
        "sematic_description": "",
        "formatted_address_poi": "",
        "cityCode": 235
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到