Class 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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidfindRoute(LatLng origin, LatLng destination, RouteCallback callback) Routes fromorigintodestinationby car and reports the outcome tocallback.static voidfindRoute(RouteRequest request, RouteCallback callback) Routesrequestand reports the outcome tocallback.static RouteServiceThe service backing every routing call, creating the default keylessOsrmRouteServiceon first use.static voidsetService(RouteService service) Installs the service backing every routing call.static voidshowRoute(MapSurface map, LatLng origin, LatLng destination) Draws the best route betweenoriginanddestinationonmapand frames it, with no further code required.static voidshowRoute(MapSurface map, RouteRequest request, RouteCallback callback) Draws the best route forrequestonmap, frames it, and forwards the outcome tocallback.
-
Method Details
-
getService
The service backing every routing call, creating the default keylessOsrmRouteServiceon first use. -
setService
Installs the service backing every routing call. Passnullto restore the defaultOsrmRouteService. -
findRoute
Routes fromorigintodestinationby car and reports the outcome tocallback. -
findRoute
Routes
requestand reports the outcome tocallback.Returns immediately;
callbackis 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 checkRouteService.isAvailable()themselves.Throws
IllegalArgumentExceptionwhencallbackisnull; an asynchronous call has no other way to reach you. -
showRoute
Draws the best route between
originanddestinationonmapand 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: the map to draw on -
origin: where the journey starts -
destination: where the journey ends
-
-
showRoute
Draws the best route for
requestonmap, frames it, and forwards the outcome tocallback.The polyline is added and the camera moved before
callbackruns, 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, andRoute.toPolyline()hands back a fresh one every call. To control how the route looks, skip this method: callfindRoute(RouteRequest, RouteCallback)and add the styled polyline yourself.Parameters
-
map: the map to draw on -
request: the journey to route -
callback: notified of the outcome, ornullto just draw the route
-
-