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

示例

相关推荐
瑞士卷@3 小时前
MyBatis入门到精通(Mybatis学习笔记)
java·数据库·后端·mybatis
梵得儿SHI3 小时前
Java 反射机制深度剖析:性能与安全性的那些坑
java·开发语言·安全·反射·动态代理·性能·反射机制
虫小宝3 小时前
Java分布式架构下的电商返利APP技术选型与架构设计实践
java·分布式·架构
007php0073 小时前
百度面试题解析:Zookeeper、ArrayList、生产者消费者模型及多线程(二)
java·分布式·zookeeper·云原生·职场和发展·eureka·java-zookeeper
4Forsee4 小时前
【Android】浅析 Android 的 IPC 跨进程通信机制
android·java
来旺4 小时前
互联网大厂Java面试全解析及三轮问答专项
java·数据库·spring boot·安全·缓存·微服务·面试
Json____4 小时前
使用node Express 框架框架开发一个前后端分离的二手交易平台项目。
java·前端·express
since �4 小时前
前端转Java,从0到1学习教程
java·前端·学习
詩句☾⋆᭄南笙4 小时前
Mybatis一对一、一对多
java·mybatis·resulttype·resultmap·一对多·一对一
Andya_net4 小时前
Java | 基于redis实现分布式批量设置各个数据中心的服务器配置方案设计和代码实践
java·服务器·分布式