Now I tried to calculate the distance using PostGIS. I used the coordinate system EPSG:4326. PostGIS differentiates between spheroid and sphere when calculating the distance (ST_Length). The result for the calculation on the sphere corresponds to the information received from the API. Do you also calculate the distances based on a sphere and if so why?
I just ran the response to your request through some python to sum the individual step distances/durations, and that equals the segment distance - within a small margin of rounding error.
There are use cases where the calculation and therefore the deviations play a role.
Why does the API perform the calculation on a sphere? Has this performance reasons?
Our simplified use case: We have two locations and one destination and the shorter route allows us to choose one location. Here is an example where the calculations on the sphere or spheroid would lead to different decisions.
Comparison of the results with calculation on sphere
Location A to destination: 1753.1 (1753.061444906453 - EPSG:4326, PostGIS)
Location B to destination: 1752.6 (1752.4256782853463 - EPSG:4326, PostGIS)
Decision for location B.
Comparison of the results with calculation on spheroid
Location A to destination: 1756.7763399376963 - EPSG:4326, PostGIS
Location B to destination: 1757.2482782162165 - EPSG:4326, PostGIS
Decision for location A.