zoukankan      html  css  js  c++  java
  • 根据地址 得到坐标

    Map<String, String> address = LatitudeUtils
           .getGeocoderLatitude(kes.getAddress());
         try {
          if (address.containsKey("lng")
            && address.containsKey("lat")) {
           final String lon = address.get("lng");
           final String lat = address.get("lat");
           ke.setLatitude(lat);
           ke.setLongitude(lon);
          }
         } catch (NullPointerException ex) {

          ke.setLatitude("");
          ke.setLongitude("");
         }

    public class LatitudeUtils {
    public static final String KEY_1 = "173bdc0916ac8c0c6fb1d95e799242b1";
     
     
     
     public static Map<String,String> getGeocoderLatitude(String address){
      BufferedReader in = null;
      try {
       
       address = URLEncoder.encode(address, "UTF-8");

       URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+ KEY_1);
       
       in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
       String res;
       StringBuilder sb = new StringBuilder("");
       while((res = in.readLine())!=null){
        sb.append(res.trim());
       }
       String str = sb.toString();
       Map<String,String> map = null;
       if(str!=""){
        int lngStart = str.indexOf("lng":");
        int lngEnd = str.indexOf(","lat");
        int latEnd = str.indexOf("},"precise");
        if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
         String lng = str.substring(lngStart+5, lngEnd);
         String lat = str.substring(lngEnd+7, latEnd);
         map = new HashMap<String,String>();
         map.put("lng", lng);
         map.put("lat", lat);
         return map;
        }
       }
      }catch (Exception e) {
       e.printStackTrace();
      }finally{
       try {
        in.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
      
      return null;
     }
    }

  • 相关阅读:
    hdu 3786 寻找直系亲属
    about the plan
    2013 ACM/ICPC Asia Regional Online —— Warmup1 1005 Balls Rearrangement
    poj 1833 给poj跪了 字典排序
    20170630总结
    20170701总结
    20170628总结
    20170629总结
    20170626总结
    20170627总结
  • 原文地址:https://www.cnblogs.com/MyBeN/p/4118919.html
Copyright © 2011-2022 走看看