The following description assumes that you have a list of GPS positions, as well as the OSRM map matching service and Overpass API available.
(1) Use OSRM Map Matching service to identify nearest roads for a trip
OSRM Map matching service accepts a list of GPS positions, or some polyline and matches it to the corresponding OSM ways. This will be the first step of the overall process and returns a list of OSM nodes ids for further processing.
I used parameter
annotation=nodes to include all OSM node ids in the result:
See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#match-service for details.
(2) Fetch ways for node ids via Overpass API
Based on the previous result, extract all OSM node ids and pass those node ids to Overpass API. The following query then retrieves all corresponding highways for those node ids:
Overpass turbo link: http://overpass-turbo.eu/s/pOT
Same query including OSM nodes from both legs in OSRM response: http://overpass-turbo.eu/s/pOW - that's actually a trip from Friedrichstraße via Torstraße to Platz der Vereinten Nationen in Berlin, Germany.
Ways according to a list of node ids
To find a way which contains all of a given list of node ids, you can use the following approach:
foreach .ways -> .singleway (
way.singleway(bn)(if:count(nodes) == allnodes.count(nodes));
overpass turbo link: http://overpass-turbo.eu/s/pPq