forked from gzzfw/backEnd/gz-dyh

wangwh
2024-08-24 0161a25576448282512683f05d2eb713dd1e3e0b
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
package cn.huge.base.common.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
 
import java.util.Map;
 
/**
 * @title: 百度地图工具类
 * @description: 百度地图工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
public class BaiduMapUtils {
 
    public static void main(String[] args) {
 
    }
 
    /**
     * 百度APi参数
     */
    private static final String ak = "roXW0LCjo6P9EnCZgG3O1cBlMzvyapwY";
    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/";
 
    /**
     * 根据详细地址获取坐标值
     * @param address 详细地址
     * @param city 城市
     * @return JSONObject
     */
    public static JSONObject getCoordinateByAddr(String address, String city) {
        Map<String, String> parameters = Maps.newHashMap();
        parameters.put("ak", ak);
        parameters.put("address", address);
        parameters.put("city", city);
        parameters.put("output", output);
        String json = HttpClientUtils.httpGet(geocodingUrl, parameters, null);
 
        JSONObject data = JSONObject.parseObject(json);
        JSONObject data1=JSONObject.parseObject(data.getString("result"));
        JSONObject data2=JSONObject.parseObject(data1.getString("location"));
        System.out.println(data2.toString());
        return data2;
    }
 
    /**
     * 根据坐标获取详细地址
     * @param lat 经度
     * @param lng 维度
     * @param coordtype 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度)
     * @return String
     */
    public static String getAddressByCoordinate(String lat, String lng, String coordtype) {
        Map<String, String> parameters = Maps.newHashMap();
        parameters.put("location", lat + "," + lng);
        parameters.put("coordtype", coordtype);
        parameters.put("ak", ak);
        parameters.put("output", output);
        String json = HttpClientUtils.httpGet(reverseUrl, parameters, null);
 
        JSONObject data = JSONObject.parseObject(json);
        JSONObject data1=JSONObject.parseObject(data.getString("result"));
        JSONObject data2=JSONObject.parseObject(data1.getString("addressComponent"));
        String district =data2.getString("district");
        System.out.println(district);
        return district;
    }
 
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */