欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 根据经纬度获取地址实现代码

程序员文章站 2024-02-21 13:53:10
 java 根据经纬度获取地址实现代码 实现代码: public class getlocation { public static void...

 java 根据经纬度获取地址实现代码

实现代码:

public class getlocation { 
  public static void main(string[] args) { 
    // lat 39.97646    
    //log 116.3039  
    string add = getadd("116.3039", "39.97646"); 
    jsonobject jsonobject = jsonobject.fromobject(add); 
    jsonarray jsonarray = jsonarray.fromobject(jsonobject.getstring("addrlist")); 
    jsonobject j_2 = jsonobject.fromobject(jsonarray.get(0)); 
    string alladd = j_2.getstring("admname"); 
    string arr[] = alladd.split(","); 
    system.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n区:"+arr[2]); 
  } 
   
  public static string getadd(string log, string lat ){ 
    //lat 小 log 大 
    //参数解释: 纬度,经度 type 001 (100代表道路,010代表poi,001代表门址,111可以同时显示前三项) 
    string urlstring = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010"; 
    string res = "";   
    try {   
      url url = new url(urlstring);  
      java.net.httpurlconnection conn = (java.net.httpurlconnection)url.openconnection();  
      conn.setdooutput(true);  
      conn.setrequestmethod("post");  
      java.io.bufferedreader in = new java.io.bufferedreader(new java.io.inputstreamreader(conn.getinputstream(),"utf-8"));  
      string line;  
      while ((line = in.readline()) != null) {  
        res += line+"\n";  
     }  
      in.close();  
    } catch (exception e) {  
      system.out.println("error in wapaction,and e is " + e.getmessage());  
    }  
    system.out.println(res); 
    return res;  
  } 
   
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!