mta.bus.vehicles() with a route ID, extract latitude and longitude from each vehicle, and display them in a list or on a map.
1
Find your route ID
MTA bus route IDs combine a borough prefix with the route number. The prefix identifies which borough the route primarily serves.
Some common examples:
Pass the route ID as a string, using the exact casing shown above (e.g.,
'Bx12', not 'BX12').2
Call mta.bus.vehicles()
Initialize the
MTA client and call mta.bus.vehicles() with the route you want to track.3
Process vehicle positions
The response contains a Example response:
vehicles array. Map over it to extract each bus’s position and relevant metadata.4
Display on a map or list
Use the For a simple text list, sort vehicles by proximity to a reference point using the Haversine formula or a geospatial library.
lat and lon values to place markers on a mapping library of your choice. The bearing field gives the vehicle’s heading in degrees (0 = north, 90 = east), which you can use to rotate a bus icon.Complete example
Occupancy status
TheoccupancyStatus field reflects how full a bus is, based on passenger load data reported by the vehicle. The possible values follow the GTFS-RT occupancy standard:
Not all vehicles report occupancy data. When the field is absent or
null, omit it from your UI rather than showing a default value.