Stops for specific route

Skip to first unread message

Ionascu Mihai

Jun 22, 2021, 11:35:03 AMJun 22
to MBTA Developers
Hello everyone. I am currently trying to retrieve all stops for a given routeId and directionId, as I am build a visual representation of this path and add the stations along the way as google markers. My problem is that by calling this API:

I'm getting absolutely ALL the stops for route "4", not only the ones for my given directionId. I suppose my approach is probably wrong. Can somebody spot my mistake? Thanks in advance.
Note: this works fine for route 1.

Ionascu Mihai

Jun 22, 2021, 3:58:56 PMJun 22
to MBTA Developers

I managed to solve my problem by retrieving the first route for a given bus label. 
In this response you can also find the shapeId for this route, so you can just call "". This returns an encoded polyline which you can decode and retrieve lat/lng tuples. With these two together you can build one of the routes for a given bus number (label), it isn't the only one of course but it's a decent solution I found.

Developer at MBTA

Jun 24, 2021, 3:48:34 PMJun 24
to MBTA Developers
Hi there,

As you've probably figured out, there isn't necessarily such a thing as "the" stops/shape for a given route, since many routes (especially bus routes) have multiple patterns of service, or don't visit the same stops in each direction. There is, however, a better way than looking at individual trips: we group trips together into "route patterns", where all the trips in a route pattern have the same stops/shape. Route patterns themselves have a descriptive name and other attributes that may be useful — check under "RoutePatternResource" in the documentation.

The relationships are like this:
  • Route has many route_patterns
  • RoutePattern has many trips
  • RoutePattern has one representative_trip
  • Trip has one shape and many stops
The representative_trip is just one trip chosen arbitrarily from the pattern's trips, which allows you to access the shape and stops more conveniently if you only care about those. We actually have an example of this on our Best Practices page (section "Nested includes"), where we fetch a route plus all of its patterns and their shapes in one request. You could expand on this to also fetch the stops of each pattern.

Reply all
Reply to author
0 new messages