zoukankan      html  css  js  c++  java
  • java如何利用google map api V3进行地址解析、反向地址解析

    转自http://coolzhi.iteye.com/blog/854368

    google map api的V3版本已经出来好一阵子了,但是由于工作忙,所以一直没有时间去捣腾它,最近看了一下,官方说V2快不能用了,提示尽快升级到V3版本,于是赶紧学习了一下,因为刚好我的网站有使用到google map api。

          V3版本已经不用再申请key了,当然,一如既往是免费的,除非你是要使用Premier版本。没有了域名限制,所以使用起来方便多了,以前是本地的和远程的都要使用不同的key,更新前不小心忘记改过来的话导致使用不了话,有时还真的想不起是这个问题。

           V3版本把地址解析和反向地址解析集成到了 Google Maps API Web Services里,这是一个为您的地图应用程序提供地理数据的Google服务的 HTTP 接口集合,其中还包括路线查询服务、海拔数据查询服务、位置查询服务等。

    下面是通过调用google map api的接口实现地址解析和反向地址解析的java代码,返回结果格式我选择了json,然后通过json-lib来实现把json字符串转换成java对象。

    /**
      * 地址解析
      * @param address 地址
      * @return 经纬度,结果形如:lat,lng
      */
     public static String getLatLngByAddress(String address){
      String latLng = "";
      BufferedReader in= null;
      try {
       URL url = new URL("http://maps.google.com/maps/api/geocode/json?address="+URLEncoder.encode(address,"UTF-8")+"&language=zh-CN&sensor=true");
       HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
       httpConn.setDoInput(true);  
       in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));//注意,这里要声明字符编码是UTF-8,否则会乱码  
          String line;
          String result="";
          while ((line = in.readLine()) != null) {  
              result += line;  
          }  
          in.close();
          JSONObject jsonObject = JSONObject.fromObject( result );
          GoogleMapJSONBean bean = (GoogleMapJSONBean) JSONObject.toBean( jsonObject, GoogleMapJSONBean.class );
          latLng = bean.getResults()[0].getGeometry().getLocation().lat+","+bean.getResults()[0].getGeometry().getLocation().lng;
          System.out.println(latLng);
      } catch (MalformedURLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } finally{
       if(in != null){
        try {
         in.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
      }
      return latLng;
     }
     
     /**
      * 反向地址解析
      * @param latLng 经纬度,格式形如:lat,lng
      * @return 地址
      */
     public static String getAddressByLatLng(String latLng){
      String address = "";
      BufferedReader in= null;
      try {
       URL url = new URL("http://maps.google.com/maps/api/geocode/json?latlng="+latLng+"&language=zh-CN&sensor=true");
       HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
       httpConn.setDoInput(true);   
       in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));//注意,这里要声明字符编码是UTF-8,否则会乱码      String line;
          String result="";
          while ((line = in.readLine()) != null) {  
              result += line;  
          }  
          in.close();
          JSONObject jsonObject = JSONObject.fromObject( result );
          GoogleMapJSONBean bean = (GoogleMapJSONBean) JSONObject.toBean( jsonObject, GoogleMapJSONBean.class );
          address = bean.getResults()[0].formatted_address;
          System.out.println("address="+address);
      } catch (MalformedURLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      return address;
     }

    附件我把整个项目上传了,里面包括了json-lib所需要用到的所有jar,你如果有需要可以下载来看看。

    这两天趁着周末,终于把自家网站的google map api由v2升级到v3了,在这一过程中,发觉我之前这里发的代码有个地方会导致反向地址解析的结果存在乱码,就是定义BufferedReader的时候,没有声明字符编码,现在这里修正一下,

    把代码中的in = new BufferedReader(new InputStreamReader(httpConn.getInputStream);

    改为:

    in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));

    附件中的代码没有修改,请下载的朋友注意自行修改,不便之处,敬请原谅!

  • 相关阅读:
    ArcGIS SilverLight/WPF API 2.0版本已正式发布,新特性
    ArcGIS Server Flex API 自定义缩放控件的级数[代码]
    First
    HTML和JavaScript代码分离、平稳退化(1)
    cocos2dx 仿射变换
    java数组创建
    第一次看CCControl
    从零开始のcocos2dx生活(四)ActionManager
    从零开始のcocos2dx生活(二)Node
    从零开始のcocos2dx生活(三)Scheduler
  • 原文地址:https://www.cnblogs.com/leiqun123/p/3167268.html
Copyright © 2011-2022 走看看