如何用JAVA獲取GOOGLE 地圖經緯度,地址信息

第一步 、申請一個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;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章