java根据经纬度获取该经纬度的省市区

来源:互联网 发布:健身行业数据 编辑:程序博客网 时间:2024/06/05 10:11
—————————————————————————————————————————————————————————————————————————————
转载自:http://blog.csdn.net/lihua5419/article/details/77964786
—————————————————————————————————————————————————————————————————————————————
[html] view plain copy
print?
  1. import java.net.URL;  
  2.   
  3.   
  4. import net.sf.json.JSONArray;  
  5. import net.sf.json.JSONObject;  
  6.   
  7. public class GetLocation {    
  8.     public static void main(String[] args) {    
  9.         // lat 31.2990170   纬度      
  10.         //log 121.3466440    经度  
  11.         String add = getAdd("121.3466440", "31.2990170");    
  12.         JSONObject jsonObject = JSONObject.fromObject(add);    
  13.         JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));    
  14.         JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));    
  15.         String allAdd = j_2.getString("admName");    
  16.         String arr[] = allAdd.split(",");    
  17.         System.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n区:"+arr[2]);    
  18.     }    
  19.         
  20.     public static String getAdd(String log, String lat ){    
  21.         //lat 小  log  大    
  22.         //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)   
  23.         String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";    
  24.         String res = "";       
  25.         try {       
  26.             URL url = new URL(urlString);      
  27.             java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();      
  28.             conn.setDoOutput(true);      
  29.             conn.setRequestMethod("POST");      
  30.             java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));      
  31.             String line;      
  32.            while ((line = in.readLine()) != null) {      
  33.                res += line+"\n";      
  34.          }      
  35.             in.close();      
  36.         } catch (Exception e) {      
  37.             System.out.println("error in wapaction,and e is " + e.getMessage());      
  38.         }     
  39.         System.out.println(res);    
  40.         return res;      
  41.     }    
  42.         
  43. }    

运行结果如下图:


需要注意的是JSONObject和JSONArray的引用是下面两个,否则会报方法JSONObject.fromObject()的错误

[html] view plain copy
print?
  1. import net.sf.json.JSONArray;  
  2. import net.sf.json.JSONObject;  

原创粉丝点击