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探秘者6 分钟前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
攸攸太上6 分钟前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
2301_7869643612 分钟前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
2303_8120444615 分钟前
Bean,看到P188没看了与maven
java·开发语言
苹果醋315 分钟前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
秋夫人17 分钟前
idea 同一个项目不同模块如何设置不同的jdk版本
java·开发语言·intellij-idea
m0_6640470222 分钟前
数字化采购管理革新:全过程数字化采购管理平台的架构与实施
java·招投标系统源码
aqua353574235842 分钟前
蓝桥杯-财务管理
java·c语言·数据结构·算法
Deryck_德瑞克42 分钟前
Java网络通信—TCP
java·网络·tcp/ip
砥砺code43 分钟前
【2024版本】Mac/Windows IDEA安装教程
java