Calculating the route using the HERE OfflineRoutingEngine based on geocoordinates

124 Views Asked by At

I need to show the Trip route on the map. To calculate a route based on an imported GeoCoordinates list, I've used the HERE SDK's OfflineRoutingEngine class. The problem is that the route returned by the calculateRoute function resembles a combination of all routes connected. Could someone please help me identify the problem with the code here?

    List<Waypoint> waypoints = new ArrayList<>();
                waypoints.add(new Waypoint(routePoints.get(0)));
                for (int i = 1; i < routePoints.size() - 1; i++) {
                    Waypoint wp = new Waypoint(routePoints.get(i));
                    wp.type =  WaypointType.PASS_THROUGH;
                    waypoints.add(wp);
                }
                waypoints.add(new Waypoint(routePoints.get(routePoints.size() - 1)));

                mOfflineRoutingEngine.calculateRoute(waypoints, new CarOptions(), (routingError, routes) -> {
                    if (routingError != null) {
                        Log.d(TAG, "showRoute(): Error while calculating a route");
                        return;
                    }
                    // When routingError is null, routes is guaranteed to contain at least one route
                    if ((routes != null) && (routes.size() > 0)) {
                        Route route = routes.get(0);
                        if (mRoutePolyline == null) {
                            Log.d(TAG, "showRoute(): addPolyline");
                            // shows routes
                            mRoutePolyline = new MapPolyline(route.getGeometry(), ROUTE_LINE_WIDTH, mRouteColor);
                            mMapView.getMapScene().addMapPolyline(mRoutePolyline);
                        } else {
                            Log.d(TAG, "showRoute(): updatedPolyline");
                            // update route
                            mRoutePolyline.setGeometry(route.getGeometry());
                        }
                    } else {
                        Log.d(TAG, "showRoute(): Error while calculating a route");
                    }
                });

I'm getting the following result as a geometry of a route: enter image description here

1

There are 1 best solutions below

2
On

If you are trying to import a route from another service or when you have a large number of waypoints that already mark a route path, use the "import route" feature of the HERE SDK:

List<Location> locations = Arrays.asList (new Location(new GeoCoordinates(52.518032,13.420632)), new Location(new GeoCoordinates(52.51772,13.42038)),
        new Location(new GeoCoordinates(52.51764,13.42062)), new Location(new GeoCoordinates(52.51754,13.42093)),
        new Location(new GeoCoordinates(52.51735,13.42155)), new Location(new GeoCoordinates(52.51719,13.42209)),
        new Location(new GeoCoordinates(52.51707,13.42248)), new Location(new GeoCoordinates(52.51695,13.42285)),
        new Location(new GeoCoordinates(52.5168, 13.42331)), new Location(new GeoCoordinates(52.51661,13.42387)),
        new Location(new GeoCoordinates(52.51648,13.42429)), new Location(new GeoCoordinates(52.51618,13.42513)),
        new Location(new GeoCoordinates(52.5161,13.42537)),  new Location(new GeoCoordinates(52.51543,13.42475)),
        new Location(new GeoCoordinates(52.51514,13.42449)), new Location(new GeoCoordinates(52.515001,13.424374)));

routingEngine.importRoute(locations, new CarOptions(), new CalculateRouteCallback() {
    @Override
    public void onRouteCalculated(@Nullable RoutingError routingError, @Nullable List<Route> list) {
        if (routingError == null) {
            Route newRoute = list.get(0);
            // ...
        } else {
            // Handle error.
        }
    }
});

This code snippet is taken from the documentation.

In your case, the image seems to show multiple waypoints that have to be reached by the OfflineRoutingEngine. The engine tries to reach all waypoints in the order of the list - so, this might explain the reason of the unexpected look.

So, before using the importRoute()-feature, it may be worth to try if you can achieve a better result by ordering the waypoints. Add the following lines:

CarOptions carOptions = new CarOptions();
carOptions.routeOptions.optimizeWaypointsOrder = true;

optimizeWaypointsOrder defaults to false, but when it is true, the engine will try to find the optimal route to connect all waypoints - ignoring the position in the list.