java經緯度、地址操做

public class LogLatUtil {

    /**
     * 根據經緯度獲取省,市,區
     * @param log
     * @param lat
     */
    public static Map<String,String> getCity(String log, String lat) {
        // lat 31.2990170   緯度
        //log 121.3466440    經度
        String add = getAdd(log, lat);
        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]);
        Map<String,String> map = new HashMap<>();
        map.put("province",arr[0]);
        map.put("city",arr[1]);
        map.put("county",arr[2]);
        return map;
    }


    /**
     * 根據地址獲取經度和緯度
     * @param addr
     * @return
     */
    public static Map<String, BigDecimal> getLatAndLngByAddress(String addr){

        String address = "";
        String lat = "";
        String lng = "";
        try {
            address = java.net.URLEncoder.encode(addr,"UTF-8");
        } catch (Exception e) {
            System.out.println("【地址轉碼】地址轉碼錯誤,address="+address);
            throw new GlobleException("【地址轉碼】地址轉碼錯誤,address="+address);
        }
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=keqEgK2e5fMflZr67mXhDiEuXvRbjAZn&callback=showLocation";
        URL myURL ;
        URLConnection httpsConn;
        try {
            myURL = new URL(url);
        } catch (Exception e) {
            System.out.println("【請求地址帶參數轉碼】請求地址帶參數轉碼錯誤");
            throw new GlobleException("【請求地址帶參數轉碼】請求地址帶參數轉碼錯誤");
        }

        InputStreamReader insr = null;
        try {
            httpsConn = myURL.openConnection();
            if (httpsConn != null) {
                insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
                BufferedReader br = new BufferedReader(insr);
                String data = null;
                if ((data = br.readLine()) != null) {
                    System.out.println("map_res:"+ URLEncoder.encode(data));
                    try {
                        lat = data.substring(data.indexOf("lat") + "lat".length()+2, data.indexOf("},\"precise\""));
                        lng = data.substring(data.indexOf("lng") + "lng".length()+2, data.indexOf(",\"lat\""));
                    }catch (Exception e){
                        throw new GlobleException("經緯度轉換錯誤,請檢查您的地址是否有誤");
                    }
                }

            }
        } catch (IOException e) {
            System.out.println("【請求失敗】請求失敗,沒法與百度地圖取得聯繫");
            throw new GlobleException("【請求失敗】請求失敗,沒法與百度地圖取得聯繫");
        }finally {
            if(insr != null){
                try {
                    insr.close();
                } catch (IOException e) {
                    throw new GlobleException("【請求失敗】請求失敗,沒法與百度地圖取得聯繫");
                }
            }
        }
        Map<String, BigDecimal> map = new HashMap();
        map.put("lat", new BigDecimal(lat));
        map.put("log", new BigDecimal(lng));
        return map;
    }

    /**
     * 根據經緯度獲取地區信息
     * @param log
     * @param lat
     * @return
     */
    private 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) {
            throw new GlobleException(e.getMessage());
        }
        System.out.println(res);
        return res;
    }


    public static void main(String[] args) {
        Map<String, BigDecimal> map = getLatAndLngByAddress("四川省新都區萬千城");
        System.out.println(map.get("lat"));
        System.out.println(map.get("log"));
    }
}
相關文章
相關標籤/搜索