通过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 小结
略
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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范式,无穷范式)