Source: https://developers.woosmap.com/products/map-api/guides/transit/

> For clean Markdown of any page, append `.md` to the page URL.

> For a complete documentation index, see https://developers.woosmap.com/llms.txt

# Get Transit Route



You can calculate transit route by using the [`woosmap.map.TransitService`](/products/map-api/reference/1.4/#woosmap.map.TransitService) object. This object is an interface to the [Route endpoint of Woosmap Transit API](/products/transit-api/route-endpoint/). 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`](/products/map-api/reference/1.4/#woosmap.map.TransitRenderer) object to render these results.

https://demo.woosmap.com/js-samples/samples/transit-advanced/app/dist/
[](https://demo.woosmap.com/js-samples/samples/transit-advanced/highlight/highlight.html "Open in new tab with highlighted code")
Try sample 

- [CodeSandbox](https://codesandbox.io/p/devbox/github/woosmap/js-samples/tree/master/dist/samples/transit-advanced/app?file=index.ts)
- [JsFiddle](https://jsfiddle.net/gh/get/library/pure/woosmap/js-samples/tree/master/dist/samples/transit-advanced/jsfiddle)
- [Clone on Github](https://github.com/Woosmap/js-samples/tree/sample/transit-advanced)

## 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`](/products/map-api/reference/1.4/#woosmap.map.transit.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:

```plaintext
{
  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](/products/map-api/reference/1.4/#woosmap.map.LatLng) or [LatLngLiteral](/products/map-api/reference/1.4/#woosmap.map.LatLngLiteral).
- `destination` (required) specifies the end location to which to calculate route. This value must be specified as a [LatLng](/products/map-api/reference/1.4/#woosmap.map.LatLng) or [LatLngLiteral](/products/map-api/reference/1.4/#woosmap.map.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:

```javascript
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](/products/transit-api/route-endpoint/#modes)

### Departure time and arrival time

Refer to the Transit API documentation for more details regarding departureTime/arrivalTime parameters: [Transit API - Route - departure\_time](/products/transit-api/route-endpoint/#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`](/products/map-api/reference/1.4/#woosmap.map.transit.TransitRouteResponse).

### Transit Route Response

The [`TransitRouteResponse`](/products/map-api/reference/1.4/#woosmap.map.transit.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:

1. Create a new `TransitRenderer` object.

2. Call `setMap()` on the renderer to link it to your map object.

3. Call `setRoutes()` on the renderer with the `routes` property of the `TransitRouteResponse` 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.

```javascript
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);
    });
```
