public class OsrmRouteService

  1. Object
  2. OsrmRouteService

ImplementsRouteService

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);

Fields

public static final String DEMO_BASE_URL = "https://router.project-osrm.org"The OSRM public demo server, used when no other base URL is configured.

Constructors

public OsrmRouteService()Creates a service pointing at the OSRM public demo server.
public OsrmRouteService(String baseUrl)Creates a service pointing at an OSRM-compatible endpoint, for example https://osrm.example.com.

Methods

public String getBaseUrl()The endpoint routes are requested from, without a trailing slash.
public OsrmRouteService setBaseUrl(String baseUrl)Points this service at an OSRM-compatible endpoint.
public String getId()A short identifier for this backend, for example "osrm".
public boolean isAvailable()Whether this service can route right now.
public void findRoutes(RouteRequest request, RouteCallback callback)Routes request and reports the outcome to callback.
public static List parseResponse(String json) throws IOExceptionParses an OSRM route service response into Route objects, best first.

Inherited methods

Field details

DEMO_BASE_URL

public static final String DEMO_BASE_URL = "https://router.project-osrm.org"
The OSRM public demo server, used when no other base URL is configured. Suitable for development and demos only – see the class documentation.

Constructor details

OsrmRouteService

public OsrmRouteService()
Creates a service pointing at the OSRM public demo server.

OsrmRouteService

public OsrmRouteService(String baseUrl)
Creates a service pointing at an OSRM-compatible endpoint, for example https://osrm.example.com.

Method details

getBaseUrl

public String getBaseUrl()
The endpoint routes are requested from, without a trailing slash.

setBaseUrl

public OsrmRouteService setBaseUrl(String baseUrl)
Points this service at an OSRM-compatible endpoint. A null or empty value restores DEMO_BASE_URL; a trailing slash is trimmed.

getId

public String getId()
A short identifier for this backend, for example "osrm". Used in log messages and to tell services apart.

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.

findRoutes

public void findRoutes(RouteRequest request, RouteCallback callback)

Routes request and reports the outcome to callback.

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.

callback is required – an asynchronous call with nowhere to report its result is a mistake, not a fire-and-forget mode, so implementations reject a null one with IllegalArgumentException rather than returning quietly and leaving the caller to wonder.

parseResponse

public static List parseResponse(String json) throws IOException

Parses an OSRM route service response into Route objects, 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 with geometries=polyline6 decodes ten times off through here; decode those yourself with PolylineCodec.decode(String, int) at precision 6.

Parameters

json String
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.