JAVA调用高德地图API实践

    高德地图api接口文档地址:https://lbs.amap.com/api/

 

  • 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;
    }

转载于:https://my.oschina.net/u/3242075/blog/3064477

你可能感兴趣的:(JAVA调用高德地图API实践)