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

示例

相关推荐
Java致死26 分钟前
设计模式Java
java·开发语言·设计模式
源码方舟28 分钟前
SpringBoot + Shiro + JWT 实现认证与授权完整方案实现
java·spring boot·后端
2401_cf4 小时前
为什么hadoop不用Java的序列化?
java·hadoop·eclipse
帮帮志4 小时前
idea整合maven环境配置
java·maven·intellij-idea
LuckyTHP4 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
无声旅者7 小时前
深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
java·ide·ai·intellij-idea·ai编程·continue·openapi
Ryan-Joee7 小时前
Spring Boot三层架构设计模式
java·spring boot
Hygge-star7 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
dkmilk7 小时前
Tomcat发布websocket
java·websocket·tomcat
工一木子8 小时前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis