基于graphhopper实现本地导航

依赖:


依赖:

  @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;
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容