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.

  1. Building Transit Route Request
  2. Displaying Transit Route

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
}

    

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, callback(response));

    

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:

  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}
    },
    (response) => {
        transitRenderer.setRoutes(response.routes);
    }
);

    
Was this article helpful?
Have more questions? Submit a request