广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cn.huge.module.utils;
 
import cn.huge.base.common.utils.HttpClientUtils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
 
import java.util.Map;
 
/**
 * @title: 百度地图工具类
 * @description: 百度地图工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
@Slf4j
public class BaiduMapUtils {
 
    public static void main(String[] args) {
        BaiduMapLngLatDTO baiduMapLngLatDTO = getLngLatByAddr("钟落潭镇五龙岗村", "广州市");
        BaiduMapAddrDTO baiduMapAddrDTO = getAddrByLngLat(baiduMapLngLatDTO.getLng(), baiduMapLngLatDTO.getLat(), BaiduMapUtils.coordtype_bd09ll);
    }
 
    /**
     * 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度)
     */
    public static final String coordtype_bd09ll = "bd09ll";
    public static final String coordtype_bd09mc = "bd09mc";
    public static final String coordtype_gcj02ll = "gcj02ll";
    public static final String coordtype_wgs84ll = "wgs84ll";
 
    /**
     * 百度APi参数
     */
    private static final String output = "json";
    private static final String callback = "showLocation";
    private static final String geocodingUrl = "http://api.map.baidu.com/geocoding/v3/";
    private static final String reverseUrl = "http://api.map.baidu.com/reverse_geocoding/v3/";
    // 李泳江个人账号:15602261488
    private static final String ak = "roXW0LCjo6P9EnCZgG3O1cBlMzvyapwY";
    // 王文豪个人账号
//    private static final String ak = "FhiGsV7BFaHGhGy2IeyPq7GuOiFroQ4o";
 
    /**
     * 根据详细地址获取坐标值
     * @param address 详细地址
     * @param city 城市
     * @return JSONObject
     */
    public static BaiduMapLngLatDTO getLngLatByAddr(String address, String city) {
        BaiduMapLngLatDTO baiduMapLngLatDTO = new BaiduMapLngLatDTO();
        try {
            String url = geocodingUrl + "?ak=" + ak + "&output=" + output + "&address=" + address + "&city=" + city;
            String json = HttpClientUtils.httpGet(url, null, null);
 
            JSONObject data = JSONObject.parseObject(json);
            JSONObject data1 = JSONObject.parseObject(data.getString("result"));
            // 解析地址的经纬度
            JSONObject lngLatComponent = JSONObject.parseObject(data1.getString("location"));
            System.out.println(lngLatComponent.toString());
            // 经度
            String lng = lngLatComponent.getString("lng");
            baiduMapLngLatDTO.setLng(lng);
            // 维度
            String lat = lngLatComponent.getString("lat");
            baiduMapLngLatDTO.setLat(lat);
        }catch (Exception e){
            log.error("[BaiduMapUtils.getLngLatByAddr]调用失败,异常信息:"+e, e);
        }
        return baiduMapLngLatDTO;
    }
 
    /**
     * 根据坐标获取详细地址
     * @param lng 经度
     * @param lat 维度
     * @param coordtype 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度)
     * @return String
     */
    public static BaiduMapAddrDTO getAddrByLngLat(String lng, String lat, String coordtype) {
        BaiduMapAddrDTO baiduMapAddrDTO = new BaiduMapAddrDTO();
        try{
            String location = lat + "," + lng;
            String url = reverseUrl+"?ak="+ak+"&output="+output+"&location="+location+"&coordtype="+coordtype;
            String json = HttpClientUtils.httpGet(url, null, null);
 
            JSONObject data = JSONObject.parseObject(json);
            JSONObject data1 = JSONObject.parseObject(data.getString("result"));
            // 识别地址
            String formattedAddress = data1.getString("formatted_address");
            System.out.println("识别地址:"+formattedAddress);
            // 解析地址组成部分
            JSONObject addressComponent = JSONObject.parseObject(data1.getString("addressComponent"));
            // 国家
            String country = addressComponent.getString("country");
            // 省
            String province = addressComponent.getString("province");
            // 市
            String city = addressComponent.getString("city");
            // 区
            String district = addressComponent.getString("district");
            // 街道
            String town = addressComponent.getString("town");
            // 路
            String street = addressComponent.getString("street");
            // 号
            String streetNumber = addressComponent.getString("street_number");
            // 拼接地址
            String addr = country+province+city+district+town+street+streetNumber;
            System.out.println("拼接地址:"+addr);
 
            baiduMapAddrDTO.setFormattedAddress(formattedAddress);
            baiduMapAddrDTO.setCountry(country);
            baiduMapAddrDTO.setProvince(province);
            baiduMapAddrDTO.setCity(city);
            baiduMapAddrDTO.setDistrict(district);
            baiduMapAddrDTO.setTown(town);
            baiduMapAddrDTO.setStreet(street);
            baiduMapAddrDTO.setStreetNumber(streetNumber);
            baiduMapAddrDTO.setAddr(addr);
        }catch (Exception e){
            log.error("[BaiduMapUtils.getAddrByLngLat]调用失败,异常信息:"+e, e);
        }
        return baiduMapAddrDTO;
    }
 
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */