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