利用GoogleApi根据经纬度查询地名,和根据地名查询经纬度Java代码

[java:showcolumns]  view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7. import java.net.URLConnection;  
  8.   
  9.   
  10. public class Demo {  
  11.  public static void main(String[] args) {  
  12.   String addr = GetAddr("35.8616600""104.1953970");  
  13.   System.out.println(addr);  
  14.   //getCoordinate("中国");  
  15.  }  
  16.   
  17.  /** 
  18.   * 根据经纬度反向解析地址,有时需要多尝试几次 
  19.   * 注意:(摘自:http://code.google.com/intl/zh-CN/apis/maps/faq.html 
  20.   * 提交的地址解析请求次数是否有限制?) 如果在 24 小时时段内收到来自一个 IP 地址超过 2500 个地址解析请求, 或从一个 IP 
  21.   * 地址提交的地址解析请求速率过快,Google 地图 API 编码器将用 620 状态代码开始响应。 如果地址解析器的使用仍然过多,则从该 IP 
  22.   * 地址对 Google 地图 API 地址解析器的访问可能被永久阻止。 
  23.   *  
  24.   * @param latitude 
  25.   *            纬度 
  26.   * @param longitude 
  27.   *            经度 
  28.   * @return 
  29.   */  
  30.  public static String GetAddr(String latitude, String longitude) {  
  31.   String addr = "";  
  32.   
  33.   // 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址  
  34.   // 密钥可以随便写一个key=abc  
  35.   // output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析  
  36.   String url = String.format(  
  37.     "http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",  
  38.     latitude, longitude);  
  39.   URL myURL = null;  
  40.   URLConnection httpsConn = null;  
  41.   try {  
  42.    myURL = new URL(url);  
  43.   } catch (MalformedURLException e) {  
  44.    e.printStackTrace();  
  45.    return null;  
  46.   }  
  47.   try {  
  48.    httpsConn = (URLConnection) myURL.openConnection();  
  49.    if (httpsConn != null) {  
  50.     InputStreamReader insr = new InputStreamReader(  
  51.       httpsConn.getInputStream(), "UTF-8");  
  52.     BufferedReader br = new BufferedReader(insr);  
  53.     String data = null;  
  54.     if ((data = br.readLine()) != null) {  
  55.      System.out.println(data);  
  56.      String[] retList = data.split(",");  
  57.      if (retList.length > 2 && ("200".equals(retList[0]))) {  
  58.       addr = retList[2];  
  59.       addr = addr.replace("/"", "");  
  60.      } else {  
  61.       addr = "";  
  62.      }  
  63.     }  
  64.     insr.close();  
  65.    }  
  66.   } catch (IOException e) {  
  67.    e.printStackTrace();  
  68.    return null;  
  69.   }  
  70.   return addr;  
  71.  }  
  72.    
  73.  public static void getCoordinate(String addr)  
  74.  {  
  75.   String addrs = "";  
  76.      String address = null;  
  77.   try {  
  78.    address = java.net.URLEncoder.encode(addr,"UTF-8");  
  79.   } catch (UnsupportedEncodingException e1) {  
  80.    e1.printStackTrace();  
  81.   };  
  82.         String output = "csv";  
  83.         String key = "abc";  
  84.         String url = String.format("http://maps.google.com/maps/geo?q=%s&output=%s&key=%s", address, output, key);  
  85.         URL myURL = null;  
  86.         URLConnection httpsConn = null;  
  87.         //进行转码  
  88.   try {  
  89.    myURL = new URL(url);  
  90.   } catch (MalformedURLException e) {  
  91.    e.printStackTrace();  
  92.   }  
  93.     
  94.   try {  
  95.    httpsConn = (URLConnection) myURL.openConnection();  
  96.    if (httpsConn != null) {  
  97.     InputStreamReader insr = new InputStreamReader(  
  98.       httpsConn.getInputStream(), "UTF-8");  
  99.     BufferedReader br = new BufferedReader(insr);  
  100.     String data = null;  
  101.     if ((data = br.readLine()) != null) {  
  102.      System.out.println(data);  
  103.      String[] retList = data.split(",");  
  104.      /* 
  105.               String latitude = retList[2]; 
  106.               String longitude = retList[3]; 
  107.                
  108.               System.out.println("纬度"+ latitude); 
  109.               System.out.println("经度"+ longitude); 
  110.               */  
  111.   
  112.      if (retList.length > 2 && ("200".equals(retList[0]))) {  
  113.       addrs = retList[2];  
  114.       addrs = addr.replace("/"", "");  
  115.      } else {  
  116.       addrs = "";  
  117.      }  
  118.     }  
  119.     insr.close();  
  120.    }  
  121.   } catch (IOException e) {  
  122.    e.printStackTrace();  
  123.   }        
  124.   System.out.println(addrs);  
  125.  }  
  126. }  
  127.   
  128.   
  129.    
  130.   
  131. ============================  
  132.   
  133. 国内的详细地址有其他地址解析服务器提供更加详细的地址解析服务,或者把数据请求后放到自己服务器,下次用户请求先从自己服务器请求,找不到地址时再去Google请求。  
  134. 来自: http://hi.baidu.com/zdz8207/blog/item/f8bbc55c36a32657faf2c025.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值