goole map 根据地址获取经纬度

 /**
     * 获取经纬度通过google
     *
     * @author jueyue 返回格式:Map<String,Object> map map.put("status",
     *         reader.nextString());//状态 map.put("result", list);//查询结果
     *         list<map<String,String>>
     */
    public static void main(String[] args) {
     // 也可以是 http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址
     // 密钥可以随便写一个key=abc
     // output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析
   String addr = "1825 sw broadway";
   String address = null;
   Map<String, Object> map = new HashMap<String, Object>();
   try {
    address = java.net.URLEncoder.encode(addr, "UTF-8");
   } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
   }
   int ipPort = (int) Math.round((Math.random() * 190));
   String key = "zh-CN";
   String url = String
     .format(" http://ditu.google.cn/maps/api/geocode/json?address=%s&sensor=false&language=%s",
       address, key);
   URL myURL = null;
   URLConnection httpsConn = null;
   try {
    myURL = new URL(url);
    httpsConn = (URLConnection) myURL.openConnection();
    if (httpsConn != null) {
     InputStreamReader insr = new InputStreamReader(
       httpsConn.getInputStream(), "UTF-8");
//     BufferedReader bf = new BufferedReader(insr);
//     StringBuffer sBuffer = new StringBuffer();
//     String data = "";
//     while ((data = bf.readLine())!= null) {
//      sBuffer.append(data+"\r\n");
//     }
//     System.out.println(sBuffer.toString());
     JsonReader reader = new JsonReader(insr);
     reader.beginObject();
     while (reader.hasNext()) {
      String tagName = reader.nextName();
      if (tagName.equals("results")) {
       reader.beginArray();
       List<Map<String, String>> list = new ArrayList<Map<String, String>>();
       while (reader.hasNext()) {
        reader.beginObject();
        Map<String, String> map_temp = new HashMap<String, String>();
        while (reader.hasNext()) {
         tagName = reader.nextName();
         if (tagName.equals("address_components")) {
          reader.skipValue();
         } else if (tagName.equals("formatted_address")) {
          map_temp.put("address", reader.nextString());
         } else if (tagName.equals("geometry")) {
          reader.beginObject();
          while (reader.hasNext()) {
           tagName = reader.nextName();
           if (tagName.equals("location")) {
            reader.beginObject();
            while (reader.hasNext()) {
             map_temp.put(reader.nextName(),
               reader.nextString());
            }
            reader.endObject();
           } else {
            reader.skipValue();
           }
          }
          reader.endObject();
         } else {
          reader.skipValue();
         }
        }
        list.add(map_temp);
        reader.endObject();
       }
       map.put("result", list);
       reader.endArray();
      } else if (tagName.equals("status")) {
       map.put("status", reader.nextString());
      }
     }
     insr.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   System.out.println(map.toString());
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值