高德地图api官方文档:https://lbs.amap.com/api/webservice/guide/api/direction#instructions
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112/** * 高德地图路经规划工具类 * @开发者 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()); }
最后
以上就是迷你金鱼最近收集整理的关于JavaWeb 道德地图路线规划工具类的全部内容,更多相关JavaWeb内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复