关闭

如何用JAVA获取GOOGLE 地图经纬度,地址信息

标签: 谷歌地图java
1475人阅读 评论(2) 收藏 举报
分类:

第一步 、申请一个GOOGLE地图的KEY

1、根据地址获取经纬度

public static void getGoogleLatLng() {
	    CloseableHttpClient httpclient = HttpClients.createDefault();
        try {  
            // 创建httpget.    
            HttpGet httpget = new HttpGet("https://maps.google.com/maps/api/geocode/json?address=上海市&sensor=false&key=");  
            logger.debug("executing request " + httpget.getURI());  
            // 执行get请求.    
            CloseableHttpResponse response = httpclient.execute(httpget);  
           
            try {  
                // 获取响应实体    
                HttpEntity entity = response.getEntity();  
                logger.debug("--------------------------------------");  
                // 打印响应状态    
                System.out.println(response.getStatusLine());  
                if (entity != null) {  
                    // 打印响应内容    
                    String str = EntityUtils.toString(entity);
                    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");
                    
                    logger.debug("lat====>>>"+o5.get("lat")+";lng=====>>>"+o5.get("lng"));
                }  
                logger.debug("------------------------------------");  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (ParseException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (IOException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.debug(e.getMessage());
            }  
        }
	}

2、根据经纬度获取地址信息

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=", lat, lng));  
            logger.debug("executing request " + httpget.getURI());  
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);  
           
            try {  
                // 获取响应实体    
                HttpEntity entity = response.getEntity();  
                logger.debug("--------------------------------------");  
                // 打印响应状态    
                System.out.println(response.getStatusLine());  
                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"));
                        logger.debug("详细地址====>>>"+addr);
                        JSONArray o3 = (JSONArray)o2.get("addressComponent");
                        logger.debug("地址明细====>>>"+JSONArray.toJSONString(o3));
                    }
                }
            } finally {
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (ParseException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (IOException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.debug(e.getMessage());
            }  
        }
        return addr;
    }


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45984次
    • 积分:774
    • 等级:
    • 排名:千里之外
    • 原创:30篇
    • 转载:0篇
    • 译文:0篇
    • 评论:34条
    最新评论