依赖:
依赖:
@Resource
private GraphHopper graphHopper;
public RouteResponse route(List<Point> points, String profile) {
//输入你的经度纬度,选择交通方式
GHRequest req = new GHRequest(points.stream()
.map(point -> new GHPoint(point.getLat(), point.getLng())).collect(Collectors.toList())).setProfile(profile);
GHResponse rsp = graphHopper.route(req);
//如果路线不可达抛异常
if (rsp.hasErrors()) {
throw new BizException("路线不可达!");
}
// use the best path, see the GHResponse class for more possibilities.
ResponsePath path = rsp.getBest();
// 导航结果点位集合
PointList pointList = path.getPoints();
// 总距离 米
double distance = path.getDistance();
// 总耗时 秒
long timeInMs = (path.getTime()) / 1000;
RouteResponse response = new RouteResponse();
response.setDistance(distance);
response.setRemainingTime(timeInMs);
List<Point> route = new ArrayList<>();
response.setRoute(route);
for (int i = 0; i < pointList.size(); i++) {
route.add(new Point(pointList.getLat(i), pointList.getLon(i)));
}
return response;
}