public class GoogleMapDemo {java public static void main(String[] args) { getGoogleLatLng("广州市天河区高普路"); String addr = getGoogleAddres(new BigDecimal(23.1716124),new BigDecimal(113.4106579)); System.err.println("addr---------------------------------"+addr);json } public static void getGoogleLatLng(String address) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 建立httpget. HttpGet httpget = new HttpGet("https://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=false&key=AIzaSyA3a5PwotknVyiN7de6eSZkxkLLpd8hm6k"); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { // 打印响应内容 String str = EntityUtils.toString(entity,"utf-8"); JSONObject o = (JSONObject) JSON.parse(str); JSONArray o2 = (JSONArray) o.get("results"); JSONObject o3 = (JSONObject) o2.get(0); JSONObject o4 = (JSONObject) o3.get("geometry"); JSONObject o5 = (JSONObject)o4.get("location"); //得到打印结果 System.err.println("lat====>>>"+o5.get("lat")+";lng=====>>>"+o5.get("lng")); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); System.err.println(e.getMessage()); }catch (IOException e) { e.printStackTrace(); System.err.println(e.getMessage()); } finally { httpclient.close(); } } //根据经度、纬度获取地址详细信息 public static String getGoogleAddres(BigDecimal lat, BigDecimal lng) { String addr = ""; if(null == lat || null == lng){ return addr; } CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 建立httpget. ,参数还能够换成英文的。 HttpGet httpget = new HttpGet(MessageFormat.format("https://maps.google.com/maps/api/geocode/json?latlng={0},{1}&sensor=false&&language=zh-CN&key=AIzaSyA3a5PwotknVyiN7de6eSZkxkLLpd8hm6k", lat, lng)); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { // 打印响应内容 String str = EntityUtils.toString(entity); JSONObject o = (JSONObject) JSON.parse(str); JSONArray o1 = (JSONArray)o.get("results"); JSONObject o2 = (JSONObject)o1.get(0); if(null != o2){ addr = String.valueOf(o2.get("formatted_address")); } } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpclient.close(); } return addr; } api |
说明:这是在java后台还处理google map的相关功能,经过httpClient直接发请求,但这种方式可能性能比较卡,特别须要网络的支持,通过测试在大陆测试时,常常出现不能链接问题,在相关跑一样的代码就一直正常。因此这种功能要看状况使用。网络