240
世界上有10种人,一种懂二进制,另一种不懂二进制。

通过ip获取省份城市名称(腾讯地图apis)

1 前言

2 代码

复制代码
public class AddressUtil {

    private static String key = "your api key“;
    private static String urlByIp = "http://apis.map.qq.com/ws/location/v1/ip?key=" + key;

    public static Map<String, String> getLocation(String ip) {
        String realUrl = urlByIp;
        if (ip != null) {
            realUrl = urlByIp + "&ip=" + ip;
        }

        Map<String, String> map = new HashMap<>();
        String resp = RequestUtils.get(realUrl);
//        {
//            "status": 0,
//                "message": "query ok",
//                "result": {
//            "ip": "240e:37a:4469:d600:1ce4:4e71:8fe1:86f5",
//                    "location": {
//                "lat": 24.87389,
//                        "lng": 118.67587
//            },
//            "ad_info": {
//                "nation": "中国",
//                        "province": "XX省",
//                        "city": "XX市",
//                        "district": "",
//                        "adcode": 110500
//            }
//        }
//        }
        JSONObject respJson = JSON.parseObject(resp);
        if (respJson != null) {
            int status = respJson.getInteger("status");
            if (status == 0) {
                JSONObject resultJson = respJson.getJSONObject("result");
                JSONObject location = resultJson.getJSONObject("location");
                map.put("location", location.getString("lat") + "," + location.getString("lng"));
                JSONObject ad_info = resultJson.getJSONObject("ad_info");
                String province = ad_info.getString("province");
                String city = ad_info.getString("city");
                map.put("province", province);
                map.put("city", city);

                return map;
            }
        }

        return null;
    }
}


//RequestUtils.java

  public static String get(String url) {
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        String resp = responseEntity.getBody();
        return resp;
    }
复制代码

3 小结

posted @   unionline  阅读(695)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2021-02-25 nginx配置来获取用户真实IP(实测有效)
2021-02-25 抖音小店(抖店)Java restTemplate 提示param_json非json格式,请检查!
2021-02-25 非常规解决方案之Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value
2021-02-25 遍历指定文件夹内文件并拼接到一起到指定文件中
2019-02-25 [转]向量(矩阵)范式理解(0范式,1范式,2范式,无穷范式)
点击右上角即可分享
微信分享提示