google map如何根据地址查询经纬度

3 月,跳不动了?>>> hot3.png

public class GoogleMapDemo {

    public static void main(String[] args) {
        getGoogleLatLng("广州市天河区高普路");
        String addr = getGoogleAddres(new BigDecimal(23.1716124),new BigDecimal(113.4106579));
        System.err.println("addr---------------------------------"+addr);

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

说明:这是在java后台还处理google map的相关功能,通过httpClient直接发请求,但这种方式可能性能比较卡,特别需要网络的支持,经过测试在大陆测试时,经常出现不能连接问题,在相关跑同样的代码就一直正常。所以这种功能要看情况使用。

 

你可能感兴趣的:(google map如何根据地址查询经纬度)