Get Transit Route
Get distance, duration and path polyline for a pair of origin and destination, based on the recommended transit route between those two points.
You can calculate transit route by using the woosmap.map.TransitService
object. This object is an interface to the Route endpoint of Woosmap Transit API. This service compute travel distance, time and path for a pair of origin and destination.
You may either display these transit routes using custom overlays or use the woosmap.map.TransitRenderer
object to render these results.
Building Transit Route Request
To use Transit Route in the Woosmap Map JS API, create an object of type woosmap.map.TransitService
and call TransitService.route()
to initiate a request to the Route endpoint of Woosmap Transit API, passing it a TransitRouteRequest
object literal (containing at least the origin/destination LatLng) and a callback method to process the transit route results.
The TransitRouteRequest
object literal contains the following fields:
{
origin: LatLng | LatLngLiteral,
destination: LatLng | LatLngLiteral,
modes?: string[],
departureTime?: string,
arrivalTime?: string
}
origin
(required) specifies the start location from which to calculate route. This value must be specified as a LatLng or LatLngLiteral.destination
(required) specifies the end location to which to calculate route. This value must be specified as a LatLng or LatLngLiteral.modes
(optional) specifies which modes of transit to include/exclude in the response.departureTime
(optional) specifies when the travel is expected to start, should be a string encoded timestamp or a string encoded date.arrivalTime
(optional) specifies when the travel is expected to finish, should be a string encoded timestamp or a string encoded date.
Below is a sample TransitRouteRequest and a call to route()
method:
const transitRouteRequest = {
origin: {lat:48.86288,lng:2.34946},
destination: {lat:52.52457, lng:13.42347},
modes: "-ferry",
departureTime: "1717006500000"
}
let transitService;
transitService = new woosmap.map.TransitService();
transitService
.route(transitRouteRequest)
.then(callback(response))
.catch((error) => {
console.error("Error calculating transit route:", error);
});
Modes
Refer to the Transit API documentation for more details regarding the modes parameter: Transit API - Route - modes
Departure time and arrival time
Refer to the Transit API documentation for more details regarding departureTime/arrivalTime parameters: Transit API - Route - departure_time
Displaying Transit Route
Processing a transit route request with the route()
method of woosmap.map.TransitService
requires passing a callback called upon service request end.
This callback returns a TransitRouteResponse
.
Transit Route Response
The TransitRouteResponse
contains the result of the transit route query which you can display on the map using the setRoutes()
method of TransitRenderer
object.
To display it, follow these steps:
-
Create a new
TransitRenderer
object. -
Call
setMap()
on the renderer to link it to your map object. -
Call
setRoutes()
on the renderer with theroutes
property of theTransitRouteResponse
object as parameter.
The below example calculates transit route between two locations in France. The TransitRenderer
handles display of the returned polyline (and alternate routes) between the origin and destination.
const transitService = new woosmap.map.TransitService();
const transitRenderer = new woosmap.map.TransitRenderer({});
const map = new woosmap.map.Map(document.getElementById("map"), {
zoom: 7,
center: {
lat: 43.5,
lng: 2.4
}
});
transitRenderer.setMap(map);
transitService.route({
origin: {lat:48.86288,lng:2.34946},
destination: {lat:52.52457, lng:13.42347}
}).then((response) => {
transitRenderer.setRoutes(response.routes);
}).catch((error) => {
console.error("Error calculating transit route:", error);
});