根據經緯度獲取所對應的省市區

原文詳見:https://blog.csdn.net/lihua5419/article/details/77964786
這兩天準備學習一下高德地圖API,因此在網上看了這篇博客,感受挺實用的,遂記錄下來,以便後續須要。內容部分沒有更改。java

public class GetLocation {
                                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;
                                }

                                public static void main(String[] args) {
                                    String add = getAdd("116.237191", "31.913839");
                                    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]);
                                }
    }

           須要注意的是JSONObject和JSONArray的引用是:import net.sf.json.JSONArray;  
                                                                                            import net.sf.json.JSONObject;

運行結果以下:
根據經緯度獲取所對應的省市區json

相關文章
相關標籤/搜索