0

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

  • And.. how do you want it to look instead? – blackapps May 22 '22 at 07:07
  • Basically what I need is to snap the list of geocoordinates to the route on a map. Sometimes coordinates are a bit off because we are getting them from other services. Based on a Here sdk guide the routeEngine Calculate route should return the matching route. Is there other Api to get the matching coordinate on a route? – Nare Babayan May 22 '22 at 21:58
  • You did not answer my question at all. – blackapps May 23 '22 at 05:06
  • Hi , Route Matching REST API can support the your case. Could you please check following document? https://developer.here.com/documentation/route-matching/dev_guide/index.html – Younjae Park May 23 '22 at 06:56
  • I appreciate your response. Is there any way to simulate this for the offline cashed maps? – Nare Babayan May 23 '22 at 17:47
  • Hi , I am not sure what the offline cashed map means. The Route matching Rest API will return matched geocodinates information based on an input. Then you are going to draw matched geocodinates on offline cashed map data. Is this your expectation? – Younjae Park May 24 '22 at 05:36
  • You can use a REST API, but then an online connection would be required, obviously. With the HERE SDK, no REST call is required - and you can sort the waypoints (and route calculation) completely offline - without any internet connection. Although it requires "offline cached maps" / downloaded in the cache when device was online, or proactively via MapDownloader and stored permanently on the device. – Nusatad May 28 '22 at 13:42

1 Answers1

0

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.

Nusatad
  • 3,231
  • 3
  • 11
  • 17
  • Thanks for your reply. The optimizeWaypointsOrder property doesn't appear in my SDK, but I have set the CarOptions carOp = new CarOptions(); carOp.routeOptions = new RouteOptions(OptimizationMode.SHORTEST);, which I assume is the same, but it didn't make any difference. – Nare Babayan May 25 '22 at 00:27
  • No, this is not the same: https://developer.here.com/documentation/android-sdk-navigate/4.11.3.0/api_reference/com/here/sdk/routing/RouteOptions.html#optimizeWaypointsOrder - this feature was introduced with v4.11.0.0. Try to switch to a newer HERE SDK version. – Nusatad May 28 '22 at 13:40