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;
    }

示例

相关推荐
码农小卡拉7 分钟前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot
向上的车轮14 分钟前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
Dragon Wu15 分钟前
Spring Security Oauth2.1 授权码模式实现前后端分离的方案
java·spring boot·后端·spring cloud·springboot·springcloud
跳动的梦想家h21 分钟前
环境配置 + AI 提效双管齐下
java·vue.js·spring
坚持就完事了23 分钟前
Java中的集合
java·开发语言
wjhx32 分钟前
QT中对蓝牙权限的申请,整理一下
java·数据库·qt
YCY^v^36 分钟前
JeecgBoot 项目运行指南
java·学习
人间打气筒(Ada)1 小时前
jenkins基于Pipeline发布项目
java·pipeline·jenkins·流水线·ci·cd·cicd
爬山算法1 小时前
Hibernate(88)如何在负载测试中使用Hibernate?
java·后端·hibernate
自不量力的A同学1 小时前
Solon AI v3.9 正式发布:全能 Skill 爆发
java·网络·人工智能