E1ED922C1E9526DD63272D7EC5C6CB77
2021-01-20 a1dba41efa841aef8fb9f2bf2a179095ef77a9cd
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.hx.util;
 
import net.sf.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
 
/**
 * 腾讯地图工具
 * @author mgchen
 *
 */
public class TengXunMapUtil {
 
    /**key*/
    public static final String KEY_SHOP = "NDTBZ-XH6WX-TOB4C-TZFHG-2ORS3-UCBFA";
    /**签名校验*/
    public static final String SIGN = "qI6ZmfLk0IYt7PPWThwqXMjnNjjPzSaW";
 
    public static final String URL_LOCATION = "http://apis.map.qq.com/ws/geocoder/v1/?address={1}&key={2}";
    public static final String URL_CITY = "http://apis.map.qq.com/ws/geocoder/v1/?location={1}&key={2}";
    
    /**地址转换为经纬度*/
    public static String[] getLocation(String address, String key)
    {
        try{
            String str = sendGet("https://apis.map.qq.com/ws/geocoder/v1/", "address=" + URLEncoder.encode(address, "utf-8") + "&key=" + key);
            JSONObject obj = JSONObject.fromObject(str);
            int status = obj.optInt("status", -1);
            if(status == 0)
            {
                JSONObject rObj = obj.optJSONObject("result");
                if(rObj != null)
                {
                    JSONObject lObj = rObj.optJSONObject("location");
                    if(lObj != null)
                    {
                        Double lng = lObj.optDouble("lng", 0.0);
                        Double lat = lObj.optDouble("lat", 0.0);
                        if(lng != 0.0 && lat != 0.0)
                        {
                            return new String[]{lng.toString(), lat.toString()};
                        }
                        
                    }
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        
        return null;
    }
 
    /**地址转换为经纬度(签名校验)*/
    public static String[] getLocation(String address, String key,String sign) {
        try{
            sign =  MD5Util.MD5Encode("/ws/geocoder/v1?address=" + address + "&key=" + key+sign,null);
            String str = sendGet("https://apis.map.qq.com/ws/geocoder/v1", "address=" + address + "&key=" + key+"&sig="+sign);
            JSONObject obj = JSONObject.fromObject(str);
            int status = obj.optInt("status", -1);
            if(status == 0)
            {
                JSONObject rObj = obj.optJSONObject("result");
                if(rObj != null)
                {
                    JSONObject lObj = rObj.optJSONObject("location");
                    if(lObj != null)
                    {
                        Double lng = lObj.optDouble("lng", 0.0);
                        Double lat = lObj.optDouble("lat", 0.0);
                        if(lng != 0.0 && lat != 0.0)
                        {
                            return new String[]{lng.toString(), lat.toString()};
                        }
 
                    }
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
    
    /**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            System.out.println("urlNameString:"+urlNameString);
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
            // 建立实际的连接
            connection.connect();
            
//            // 获取所有响应头字段
//            Map<String, List<String>> map = connection.getHeaderFields();
//            // 遍历所有的响应头字段
//            for (String key : map.keySet()) {
//                System.out.println(key + "--->" + map.get(key));/
//            }
            
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    
    public static void main(String[] args)
    {
        System.out.println(sendGet("http://apis.map.qq.com/ws/geocoder/v1/", "address=" +"北京市海淀区彩和坊路海淀西大街74号" + "&key=" + "11"));
    }
 
    /**获取省/市*/
    public static String[] getAddress(String lat, String lng, String key)
    {
        try{
            String str = sendGet("http://apis.map.qq.com/ws/geocoder/v1/", "location=" + lat + "," + lng + "&key=" + key);
            JSONObject obj = JSONObject.fromObject(str);
            if(obj != null && obj.optInt("status", -1) == 0)
            {
                obj = obj.optJSONObject("result");
                if(obj != null)
                {
                    obj = obj.optJSONObject("address_component");
                    if(obj != null)
                    {
                        String nation = obj.optString("nation");
                        String province = obj.optString("province");
                        String city = obj.optString("city");
                        String area = obj.optString("district");
                        String detail = obj.optString("street") + obj.optString("street_number");
                        String[] addr = new String[]{
                                new String(nation.getBytes(), "utf-8"),
                                new String(province.getBytes(), "utf-8"), 
                                new String(city.getBytes(), "utf-8"),
                                new String(area.getBytes(), "utf-8"),
                                new String(detail.getBytes(), "utf-8")
                                };
                        
                        return addr;
                    }
                    
                    return null;
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}