JavaWeb 道德地图路线规划工具类

高德地图api官方文档:https://lbs.amap.com/api/webservice/guide/api/direction#instructions

 

/**
 * 高德地图路经规划工具类
 * @开发者 hankongbin
 * @文件名 GDMapNavUtil.java
 * @类名 GDMapNavUtil
 */
public class GDMapNavUtil {
    private String startCoordinate;
    private String endCoordinate;
    private String applicationKey;
    private String param;
    /**
     * 必须要构造参数
     * @param startCoordinate 起点经纬度 经度在前,纬度在后
     * @param endCoordinate 终点经纬度 经度在前,纬度在后
     * @param applicationKey 高德地图应用key,需要Web服务类型的key
     */
    public MapNavUtil(String startCoordinate, String endCoordinate,
            String applicationKey) {
        this.startCoordinate = startCoordinate;
        this.endCoordinate = endCoordinate;
        this.applicationKey = applicationKey;
        this.param="origin="+this.startCoordinate+"&destination="+this.endCoordinate+"&key="+this.applicationKey;
    }
    /**
     * 获取地图导航返回值 驾车
     * @return
     */
    public String getDriving(){
        String sendGet = HttpRequestUtil.sendPost("https://restapi.amap.com/v3/direction/driving", param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject=JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        // duration 时间:秒
        // distance 距离:米
        return zuiJson.get("distance").toString();
    }
    
    /**
     * 获取地图导航返回值 步行
     * @return
     */
    public String getWalking(){
        String sendGet = HttpRequestUtil.sendPost("https://restapi.amap.com/v3/direction/walking", param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject=JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get("distance").toString();
    }
    
    /**
     * 获取地图导航返回值 公交
     * @return
     */
    public String getIntegrated(){
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd2=new SimpleDateFormat("HH:mm");
        Date date=new Date();
        param+="&city=四川&cityd=四川&strategy=0&nightflag=0&date="+sd.format(date)+"&time="+sd2.format(date);
        String sendGet = HttpRequestUtil.sendPost("https://restapi.amap.com/v3/direction/transit/integrated", param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        JSONObject object = jsonObject.getJSONObject("route");
        return object.getString("distance");
    }
    
    /**
     * 获取地图导航返回值 骑行
     * @return
     */
    public String getBicycling(){
        String sendGet = HttpRequestUtil.sendPost("https://restapi.amap.com/v4/direction/bicycling", param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("data").toString();
        JSONObject routeObject=JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get("distance").toString();
    }
    
    
    
    
    /**
     * 获取地图导航返回值 驾车
     * 获取到达目的地所需时间
     * @return
     */
    public String getDrivingTime(){
        String sendGet = HttpRequestUtil.sendPost("https://restapi.amap.com/v3/direction/driving", param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        String routeJsonString = jsonObject.get("route").toString();
        JSONObject routeObject=JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        return zuiJson.get("duration").toString();//秒
    }
}

public static void main(String[] args) {
		// 起始地 经度lo - 维度 la
		String origin = 104.082997 + "," + 30.628522;
		// 目的地
		String destination = 104.090889 + "," + 30.530684;
		MapNavUtil m = new MapNavUtil(origin,destination,"xxx");
		System.out.println("驾车:"+m.getDrivingTime());
		System.out.println("步行:"+m.getWalkingTime());

	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值