| | |
| | | @Slf4j |
| | | public class BaiduMapUtil { |
| | | public static String URL = "https://api.map.baidu.com/place/v2/search?"; |
| | | public static String GEOCONV_URL = "https://api.map.baidu.com/geoconv/v2/?"; |
| | | |
| | | public static String AK = "3mHKIXMArjgIkgADzOlTYp4XssNSNkwr"; |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | |
| | | |
| | | Map params = new LinkedHashMap<String, String>(); |
| | | params.put("query", "银行"); |
| | | params.put("location", "39.915,116.404"); |
| | | params.put("radius", "2000"); |
| | | params.put("output", "json"); |
| | | params.put("ak", AK); |
| | | |
| | | |
| | | requestGetAK(URL, params); |
| | | //Map params = new LinkedHashMap<String, String>(); |
| | | //params.put("query", "银行"); |
| | | //params.put("location", "39.915,116.404"); |
| | | //params.put("radius", "2000"); |
| | | //params.put("output", "json"); |
| | | //params.put("ak", AK); |
| | | // |
| | | // |
| | | //requestGetAK(URL, params); |
| | | System.out.println(geoconv("116.390838,39.916245").toJSONString()); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | return jsonArray; |
| | | } |
| | | public static JSONObject requestGetAKObject(String strUrl, Map<String, String> param) throws Exception { |
| | | if (strUrl == null || strUrl.length() <= 0 || param == null || param.size() <= 0) { |
| | | return new JSONObject(); |
| | | } |
| | | |
| | | StringBuffer queryString = new StringBuffer(); |
| | | queryString.append(strUrl); |
| | | for (Map.Entry<?, ?> pair : param.entrySet()) { |
| | | queryString.append(pair.getKey() + "="); |
| | | // 第一种方式使用的 jdk 自带的转码方式 第二种方式使用的 spring 的转码方法 两种均可 |
| | | // queryString.append(URLEncoder.encode((String) pair.getValue(), "UTF-8").replace("+", "%20") + "&"); |
| | | queryString.append(UriUtils.encode((String) pair.getValue(), "UTF-8") + "&"); |
| | | } |
| | | |
| | | if (queryString.length() > 0) { |
| | | queryString.deleteCharAt(queryString.length() - 1); |
| | | } |
| | | |
| | | java.net.URL url = new URL(queryString.toString()); |
| | | System.out.println(queryString.toString()); |
| | | URLConnection httpConnection = (HttpURLConnection) url.openConnection(); |
| | | httpConnection.connect(); |
| | | |
| | | InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream()); |
| | | BufferedReader reader = new BufferedReader(isr); |
| | | StringBuffer buffer = new StringBuffer(); |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | buffer.append(line); |
| | | } |
| | | reader.close(); |
| | | isr.close(); |
| | | return JSONObject.parseObject(buffer.toString()); |
| | | } |
| | | public static JSONObject geoconv(String coords) throws Exception { |
| | | Map params = new LinkedHashMap<String, String>(); |
| | | params.put("coords", coords); |
| | | params.put("output", "json"); |
| | | params.put("model", "2"); |
| | | params.put("ak", AK); |
| | | return requestGetAKObject(GEOCONV_URL, params); |
| | | |
| | | } |
| | | |
| | | } |