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

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直接发请求,但这种方式可能性能比较卡,特别需要网络的支持,经过测试在大陆测试时,经常出现不能连接问题,在相关跑同样的代码就一直正常。所以这种功能要看情况使用。

 

转载于:https://my.oschina.net/u/1581846/blog/829577

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值