JAVA調用高德地圖API實踐

    高德地圖api接口文檔地址https://lbs.amap.com/api/web

 

  • JAVA調用高德地圖API,反解析中地址爲經緯度
/**
     * 高德地圖WebAPI : 地址轉化爲高德座標
     * String address:高德地圖地址
     * KEY-爲地圖key,這裏的key要申請對應服務的key 必定要選擇「web服務」項的key
     * 輸入:成都市武侯區
     * 輸出:104.043390,30.641982
     */
    public static String coordinate(String address) {
        try {
            address = URLEncoder.encode(address, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String url = BASE_PATH + "/geocode/geo?address=" + address + "&output=json&key="+ KEY;
        String coordinateString = null;
        try {
            String temp=HttpClientUtil.doGet(url);
            JSONObject jsonobject = JSONObject.fromObject(temp); 
            JSONArray pathArray = jsonobject.getJSONArray("geocodes");
            coordinateString = pathArray.getJSONObject(0).getString("location");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return coordinateString;
    }
  • 計算兩個經緯度之間的駕駛距離
/**
     * 高德地圖WebAPI : 駕車路徑規劃 計算兩地之間行駛的距離(米)
     * String origins:起始座標
     * String destination:終點座標
     *輸入:原座標:{116.45925,39.910031},目標座標:{116.587922,40.081577}
     *輸出:25424
     */
    public static String distance(String origins, String destination) {
        String distanceString = null;
        try {
            String url = BASE_PATH + "/direction/driving?" + "origin=" + origins + "&destination=" + destination
                    + "&output=json"+ "&key="+ KEY;
            String aa =HttpClientUtil.doGet(url);
            JSONObject jsonobject=JSONObject.fromObject(aa);
            JSONArray pathArray = jsonobject.getJSONObject("route").getJSONArray("paths");
            distanceString = pathArray.getJSONObject(0).getString("distance");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return distanceString;
    }
相關文章
相關標籤/搜索