JAVA使用高德地圖API獲取地理位置名稱/二點之間距離


  • 登陸高德開放平台,注冊賬號
  • 創建應用並獲取key

  • 創建內部鏈接方法
 /**
     * 向指定URL發送GET方法的請求
     *
     * @param url 發送請求的URL
     * @return URL 所代表遠程資源的響應結果
     */
    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接
            URLConnection connection = realUrl.openConnection();
            // 設置通用的請求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立實際的連接
            connection.connect();
            // 定義 BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            return null;
        } finally { // 使用finally塊來關閉輸入流
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
  • 根據經緯度獲取地理位置名稱
    /**
         * 逆地理編碼url
         *
         * @param location 經緯度坐標,經度在前,緯度在后,經緯度間以“,”分割
         * @return
         */
        public static String getRegeoUrl(String location) {
            return "http://restapi.amap.com/v3/geocode/regeo?location=" + location + "&key=" + key;
        }

     

    /**
         * 獲取地理位置名稱
         *
         * @param location 經緯度坐標,經度在前,緯度在后,經緯度間以“,”分割
         * @return
         */
        public static String getAddressNameByGeocoder(String location) {
            String jsonString = sendGet(getRegeoUrl(location));
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            String addressName = jsonObject.getJSONObject("regeocode").getString("formatted_address");
            return addressName;
        }
  • 獲取二個位置之間的直線距離
    /**
         * 距離測量API服務地址
         *
         * @param origins     出發點
         * @param destination 目的地
         * @return
         */
        public static String getDistanceUrl(String origins, String destination) {
            String urlString = "http://restapi.amap.com/v3/distance?type=0&origins=" + origins + "&destination=" + destination + "&key=" + key;
            return urlString;
        }
    /**
         * 獲取二個地點之間的直線距離
         * @param origins 出發地
         * @param destination 目的地
         * @return
         */
        public static String getDistance(String origins, String destination) {
            String jsonString = sendGet(getDistanceUrl(origins,destination));
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            String distance = jsonObject.getJSONArray("results").getJSONObject(0).getString("distance");
            return distance;
        }

    更多內容請參照官方API

http://lbs.amap.com/api/webservice/summary/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM