Class OsrmRouteService
- All Implemented Interfaces:
RouteService
The keyless RouteService Codename One uses by default: routing over the
OpenStreetMap road network through the
OSRM HTTP API.
Like the OpenFreeMap basemap behind MapView, it needs
no API key and no signup, so a road-following route works out of the box in
the simulator and on device.
Before you ship: the default endpoint is OSRM's public demo server. It is provided for development and demos, has no SLA, is rate limited and may refuse long routes. For production point this service at your own OSRM instance (or any OSRM-compatible endpoint) and nothing else in your code changes:
Routing.setService(new OsrmRouteService("https://osrm.example.com"));
On travel modes: the request URL carries the mode as OSRM's profile
path component, which OSRM-compatible hosted services (Mapbox Directions
and friends) use to pick a profile. A stock osrm-routed process does
not: it serves the single dataset it was prepared and started with and
ignores that part of the path. So one OsrmRouteService speaks to one
endpoint, and that endpoint answers with whatever profile it holds --
the public demo server holds the car profile, which is why
TravelMode.WALKING and TravelMode.CYCLING are accepted there but
answered with driving data. To offer several modes off self-hosted OSRM,
give each mode its own endpoint and pick the matching service:
OsrmRouteService walking = new OsrmRouteService("https://osrm-foot.example.com");
walking.findRoutes(request.setTravelMode(TravelMode.WALKING), callback);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe OSRM public demo server, used when no other base URL is configured. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a service pointing at the OSRM public demo server.OsrmRouteService(String baseUrl) Creates a service pointing at an OSRM-compatible endpoint, for examplehttps://osrm.example.com. -
Method Summary
Modifier and TypeMethodDescriptionvoidfindRoutes(RouteRequest request, RouteCallback callback) Routesrequestand reports the outcome tocallback.The endpoint routes are requested from, without a trailing slash.getId()A short identifier for this backend, for example"osrm".booleanWhether this service can route right now.static ListparseResponse(String json) Parses an OSRMrouteservice response intoRouteobjects, best first.setBaseUrl(String baseUrl) Points this service at an OSRM-compatible endpoint.
-
Field Details
-
DEMO_BASE_URL
The OSRM public demo server, used when no other base URL is configured. Suitable for development and demos only -- see the class documentation.- See Also:
-
-
Constructor Details
-
OsrmRouteService
public OsrmRouteService()Creates a service pointing at the OSRM public demo server. -
OsrmRouteService
Creates a service pointing at an OSRM-compatible endpoint, for examplehttps://osrm.example.com.
-
-
Method Details
-
getBaseUrl
The endpoint routes are requested from, without a trailing slash. -
setBaseUrl
Points this service at an OSRM-compatible endpoint. Anullor empty value restoresDEMO_BASE_URL; a trailing slash is trimmed. -
getId
A short identifier for this backend, for example"osrm". Used in log messages and to tell services apart.- Specified by:
getIdin interfaceRouteService
-
isAvailable
public boolean isAvailable()Whether this service can route right now. A service that needs an API key returns false until one is configured, letting callers fail fast with a clear message instead of a network error.
Always true -- OSRM needs no credentials.
- Specified by:
isAvailablein interfaceRouteService
-
findRoutes
Routes
requestand reports the outcome tocallback.The call returns immediately; the work happens off the event dispatch thread and the callback is invoked back on it. Implementations must invoke exactly one callback method for every request, including when the request is rejected outright.
callbackis required -- an asynchronous call with nowhere to report its result is a mistake, not a fire-and-forget mode, so implementations reject anullone withIllegalArgumentExceptionrather than returning quietly and leaving the caller to wonder.- Specified by:
findRoutesin interfaceRouteService
-
parseResponse
Parses an OSRM
routeservice response intoRouteobjects, best first.Exposed so an app that fetches from an OSRM-compatible endpoint through its own transport (a proxy, a cached response, a bundled fixture) can reuse the same parsing.
Geometry precision: this reads geometries as
geometries=polyline, the precision-5 encoding this service always requests. A response fetched withgeometries=polyline6decodes ten times off through here; decode those yourself withPolylineCodec.decode(String, int)at precision 6.Parameters
json: the raw response body
Returns
the routes found, never empty
Throws
IOException: when the body is malformed -- unparseable, or holding a route, leg or step that is not a JSON object -- or when OSRM reported that it could not route the request. Malformed input never escapes as an unchecked exception, so catching this is enough.
- Throws:
IOException
-