Java使用百度地图API根据经纬度反查位置信息

前言

一个小需求,需要查询某车某手机的位置信息,可以使用百度或者高德提供的地图API,本文以百度地图API为例。

获取ak

这是第一步,我们登录百度地图,创建应用获取ak

API接口地址

https://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194

入参出参传送门

方法实现

java 复制代码
/**
*逆地理编码方法
*lat 纬度
*lng 经度
*/
public static TbStation getBaiDuDecoding(Double lat, Double lng) {
        TbStation tbStation = new TbStation();
        try {
            String ak = 您的ak;
            String url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=%s&output=json&coordtype=wgs84ll&ret_coordtype=wgs84ll&location=%s,%s&extensions_poi=1&extensions_town=true";

            url = String.format(url, ak, lat, lng);
            String res = HttpUtils.doGet(url, null, String.class);
            if (null == res || "".equals(res)) return null;
            JSONObject locationObject = JSON.parseObject(res).getJSONObject("result");
            if (locationObject != null) {
                JSONObject addressComponent = locationObject.getJSONObject("addressComponent");
                if (addressComponent != null) {
                    tbStation.setProvince(addressComponent.getString("province"));
                    tbStation.setCity(addressComponent.getString("city"));
                    tbStation.setDistrict(addressComponent.getString("district"));
                    tbStation.setTownship(addressComponent.getString("town"));
                    tbStation.setStreet(addressComponent.getString("street"));
                }
                JSONArray jsonObjectList = locationObject.getJSONArray("pois");
                if (jsonObjectList.size() > 0) {
                    int i = 0;
                    JSONObject jsonHouse = jsonObjectList.getJSONObject(i);
                    while (isNumeric(jsonHouse.getString("name").substring(0, 1))) {
                        jsonHouse = jsonObjectList.getJSONObject(i++);
                    }
                    JSONObject poiLatlng = jsonHouse.getJSONObject("point");
                    tbStation.setLat(poiLatlng.getString("y"));
                    tbStation.setLng(poiLatlng.getString("x"));
                    tbStation.setHouse(jsonHouse.getString("name"));
                    tbStation.setDistance(jsonHouse.getString("distance"));
                    tbStation.setLocation(jsonHouse.getString("location"));
                    tbStation.setKindType(jsonHouse.getString("poiType"));
                    tbStation.setTag(jsonHouse.getString("tag"));
                    tbStation.setAddress(jsonHouse.getString("addr"));
                } else {
                    tbStation.setHouse("附近无POI");
                    tbStation.setAddress(locationObject.getString("formatted_address"));
                }
            } else {
                log.error("===>调用地图api失败<=====,返回消息:{}", res);
            }
        } catch (Exception e) {
            log.error("getBaiDuCoding error ---------- " + e.getMessage());
        }
        return tbStation;
    }

示例

相关推荐
冬夜戏雪1 小时前
【java学习日记】【2025.12.7】【7/60】
java·开发语言·学习
CC.GG1 小时前
【C++】二叉搜索树
java·c++·redis
JIngJaneIL2 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
南部余额3 小时前
踩坑与解惑:深入理解 SpringBoot 自动配置原理与配置排除机制
java·spring boot·自动配置原理·import
木鹅.4 小时前
LangChain4j
java
永远都不秃头的程序员(互关)4 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
CoderYanger5 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
代码栈上的思考5 小时前
深入解析Spring IoC核心与关键注解
java·后端·spring
Mai Dang5 小时前
SpringBoot4用Swagger
java
geekmice6 小时前
实现一个功能:springboot项目启动将controller地址拼接打印到txt文件
java·spring boot·后端