fhx
2023-04-06 c1a9eee9c9aa0d3c7592fa75e70243e0297b3a0d
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import net.sf.json.JSONObject;
4
5 import java.io.BufferedReader;
6 import java.io.InputStreamReader;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.net.URLEncoder;
10
11 /**
12  * 腾讯地图工具
13  * @author mgchen
14  *
15  */
16 public class TengXunMapUtil {
17
18     /**key*/
19     public static final String KEY_SHOP = "NDTBZ-XH6WX-TOB4C-TZFHG-2ORS3-UCBFA";
20     /**签名校验*/
21     public static final String SIGN = "qI6ZmfLk0IYt7PPWThwqXMjnNjjPzSaW";
22
23     public static final String URL_LOCATION = "http://apis.map.qq.com/ws/geocoder/v1/?address={1}&key={2}";
24     public static final String URL_CITY = "http://apis.map.qq.com/ws/geocoder/v1/?location={1}&key={2}";
25     
26     /**地址转换为经纬度*/
27     public static String[] getLocation(String address, String key)
28     {
29         try{
30             String str = sendGet("https://apis.map.qq.com/ws/geocoder/v1/", "address=" + URLEncoder.encode(address, "utf-8") + "&key=" + key);
31             JSONObject obj = JSONObject.fromObject(str);
32             int status = obj.optInt("status", -1);
33             if(status == 0)
34             {
35                 JSONObject rObj = obj.optJSONObject("result");
36                 if(rObj != null)
37                 {
38                     JSONObject lObj = rObj.optJSONObject("location");
39                     if(lObj != null)
40                     {
41                         Double lng = lObj.optDouble("lng", 0.0);
42                         Double lat = lObj.optDouble("lat", 0.0);
43                         if(lng != 0.0 && lat != 0.0)
44                         {
45                             return new String[]{lng.toString(), lat.toString()};
46                         }
47                         
48                     }
49                 }
50             }
51         }catch(Exception e)
52         {
53             e.printStackTrace();
54         }
55         
56         return null;
57     }
58
59     /**地址转换为经纬度(签名校验)*/
60     public static String[] getLocation(String address, String key,String sign) {
61         try{
62             sign =  MD5Util.MD5Encode("/ws/geocoder/v1?address=" + address + "&key=" + key+sign,null);
63             String str = sendGet("https://apis.map.qq.com/ws/geocoder/v1", "address=" + address + "&key=" + key+"&sig="+sign);
64             JSONObject obj = JSONObject.fromObject(str);
65             int status = obj.optInt("status", -1);
66             if(status == 0)
67             {
68                 JSONObject rObj = obj.optJSONObject("result");
69                 if(rObj != null)
70                 {
71                     JSONObject lObj = rObj.optJSONObject("location");
72                     if(lObj != null)
73                     {
74                         Double lng = lObj.optDouble("lng", 0.0);
75                         Double lat = lObj.optDouble("lat", 0.0);
76                         if(lng != 0.0 && lat != 0.0)
77                         {
78                             return new String[]{lng.toString(), lat.toString()};
79                         }
80
81                     }
82                 }
83             }
84         }catch(Exception e) {
85             e.printStackTrace();
86         }
87
88         return null;
89     }
90     
91     /**
92      * 向指定URL发送GET方法的请求
93      * 
94      * @param url
95      *            发送请求的URL
96      * @param param
97      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
98      * @return URL 所代表远程资源的响应结果
99      */
100     public static String sendGet(String url, String param) {
101         String result = "";
102         BufferedReader in = null;
103         try {
104             String urlNameString = url + "?" + param;
105             System.out.println("urlNameString:"+urlNameString);
106             URL realUrl = new URL(urlNameString);
107             // 打开和URL之间的连接
108             URLConnection connection = realUrl.openConnection();
109             // 设置通用的请求属性
110             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");
111             connection.setRequestProperty("connection", "Keep-Alive");
112             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");
113             // 建立实际的连接
114             connection.connect();
115             
116 //            // 获取所有响应头字段
117 //            Map<String, List<String>> map = connection.getHeaderFields();
118 //            // 遍历所有的响应头字段
119 //            for (String key : map.keySet()) {
120 //                System.out.println(key + "--->" + map.get(key));/
121 //            }
122             
123             // 定义 BufferedReader输入流来读取URL的响应
124             in = new BufferedReader(new InputStreamReader(
125                     connection.getInputStream()));
126             String line;
127             while ((line = in.readLine()) != null) {
128                 result += line;
129             }
130         } catch (Exception e) {
131             System.out.println("发送GET请求出现异常!" + e);
132             e.printStackTrace();
133         }
134         // 使用finally块来关闭输入流
135         finally {
136             try {
137                 if (in != null) {
138                     in.close();
139                 }
140             } catch (Exception e2) {
141                 e2.printStackTrace();
142             }
143         }
144         return result;
145     }
146     
147     public static void main(String[] args)
148     {
149         System.out.println(sendGet("http://apis.map.qq.com/ws/geocoder/v1/", "address=" +"北京市海淀区彩和坊路海淀西大街74号" + "&key=" + "11"));
150     }
151
152     /**获取省/市*/
153     public static String[] getAddress(String lat, String lng, String key)
154     {
155         try{
156             String str = sendGet("http://apis.map.qq.com/ws/geocoder/v1/", "location=" + lat + "," + lng + "&key=" + key);
157             JSONObject obj = JSONObject.fromObject(str);
158             if(obj != null && obj.optInt("status", -1) == 0)
159             {
160                 obj = obj.optJSONObject("result");
161                 if(obj != null)
162                 {
163                     obj = obj.optJSONObject("address_component");
164                     if(obj != null)
165                     {
166                         String nation = obj.optString("nation");
167                         String province = obj.optString("province");
168                         String city = obj.optString("city");
169                         String area = obj.optString("district");
170                         String detail = obj.optString("street") + obj.optString("street_number");
171                         String[] addr = new String[]{
172                                 new String(nation.getBytes(), "utf-8"),
173                                 new String(province.getBytes(), "utf-8"), 
174                                 new String(city.getBytes(), "utf-8"),
175                                 new String(area.getBytes(), "utf-8"),
176                                 new String(detail.getBytes(), "utf-8")
177                                 };
178                         
179                         return addr;
180                     }
181                     
182                     return null;
183                 }
184             }
185         }catch(Exception e)
186         {
187             e.printStackTrace();
188         }
189         return null;
190     }
191 }