public final class Routing

  1. Object
  2. Routing

The entry point for road-following routes.

A Polyline joins the points you give it with straight lines. To draw the road a driver would actually take you need a routing service to work out the road geometry first, which is what this class does:

MapView map = new MapView();
Routing.showRoute(map, new LatLng(38.8977, -77.0365), new LatLng(38.8894, -77.0352));

Routing is asynchronous – the call returns immediately and the line appears once the service answers. For control over the result use findRoute(RouteRequest, RouteCallback):

Routing.findRoute(new RouteRequest(origin, destination), new RouteCallback() {
    public void routesFound(List routes) {
        Route best = (Route)routes.get(0);
        map.addPolyline(best.toPolyline().setStrokeColor(0xff5722));
        map.fitBounds(best.getBounds(), 40);
        distanceLabel.setText((int)(best.getDistanceMeters() / 1000) + " km");
    }

    public void routeFailed(String message, Throwable error) {
        ToastBar.showErrorMessage(message);
    }
});

The work is done by a RouteService. Unless you install another, that is the keyless OsrmRouteService – read its documentation before shipping, since its default endpoint is a public demo server.

Methods

public static synchronized RouteService getService()The service backing every routing call, creating the default keyless OsrmRouteService on first use.
public static synchronized void setService(RouteService service)Installs the service backing every routing call.
public static void findRoute(LatLng origin, LatLng destination, RouteCallback callback)Routes from origin to destination by car and reports the outcome to callback.
public static void findRoute(RouteRequest request, RouteCallback callback)Routes request and reports the outcome to callback.
public static void showRoute(MapSurface map, LatLng origin, LatLng destination)Draws the best route between origin and destination on map and frames it, with no further code required.
public static void showRoute(MapSurface map, RouteRequest request, RouteCallback callback)Draws the best route for request on map, frames it, and forwards the outcome to callback.

Inherited methods

Method details

getService

public static synchronized RouteService getService()
The service backing every routing call, creating the default keyless OsrmRouteService on first use.

setService

public static synchronized void setService(RouteService service)
Installs the service backing every routing call. Pass null to restore the default OsrmRouteService.

findRoute

public static void findRoute(LatLng origin, LatLng destination, RouteCallback callback)
Routes from origin to destination by car and reports the outcome to callback.

findRoute

public static void findRoute(RouteRequest request, RouteCallback callback)

Routes request and reports the outcome to callback.

Returns immediately; callback is invoked later on the event dispatch thread, exactly once. A service that reports itself unavailable – one still waiting for an API key, say – fails the request here with a readable message instead of letting it turn into a network error, so callers never have to check RouteService.isAvailable() themselves.

Throws IllegalArgumentException when callback is null; an asynchronous call has no other way to reach you.

showRoute

public static void showRoute(MapSurface map, LatLng origin, LatLng destination)

Draws the best route between origin and destination on map and frames it, with no further code required.

This is the least code that gets you from two coordinates to a line following the roads. Failures are silent – when you need to report them, or to style the line, use showRoute(MapSurface, RouteRequest, RouteCallback).

Parameters

map MapSurface
the map to draw on
origin LatLng
where the journey starts
destination LatLng
where the journey ends

showRoute

public static void showRoute(MapSurface map, RouteRequest request, RouteCallback callback)

Draws the best route for request on map, frames it, and forwards the outcome to callback.

The polyline is added and the camera moved before callback runs, so the callback can read the route’s distance and duration to update the UI. It cannot restyle the line that was drawn – that polyline is not exposed, and Route.toPolyline() hands back a fresh one every call. To control how the route looks, skip this method: call findRoute(RouteRequest, RouteCallback) and add the styled polyline yourself.

Parameters

map MapSurface
the map to draw on
request RouteRequest
the journey to route
callback RouteCallback
notified of the outcome, or null to just draw the route