Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox Matrix Distance is off between two points

I'm using Mapbox Matrix API for the first time. I'm trying to get the driving distance between two points:

Point A -105.044052,40.541783
Point B -105.43421,41.2337

My request is in this format:

https://api.mapbox.com/directions-matrix/v1/mapbox/driving/-105.044052,40.5417839;-105.434219,41.233714?sources=0&access_token=xyz

The response I'm getting is:

{"code":"Ok",
 "durations":[[0.0,4687.2]],
 "destinations":[
   {
     "distance":25.678536277,
     "name":"Carlton Avenue",
     "location":[-105.044287,40.541638]
   },
   { 
     "distance":199.410588356,
     "name":"",
     "location":[-105.436598,41.233736]
   }
 ],
 "sources":[
   {
     "distance":25.678536277,
     "name":"Carlton Avenue",
     "location":[-105.044287,40.541638]
   }
 ]
}

My question is regarding distance results. The API says it returns all distances in meters. 199 meters is less than 1 mile. The two points should be around 80 miles apart. The duration of 4687 seconds sounds correct. That's 1.3 hours.

So why is the distance off?

like image 200
user1907377 Avatar asked May 24 '26 16:05

user1907377


2 Answers

Is there a reason why you're using the Matrix API? If you'll always just need the distance between two lat/long coordinates, I'd encourage you to look into using the Mapbox Directions API.

See https://docs.mapbox.com/playground/directions as well.

like image 76
langsmith Avatar answered May 30 '26 11:05

langsmith


The odd-looking distances that are being returned are actually the distance the coordinates are from the end of the computed route. The southern point's coordinate is inside the building but the route starts 25.679m away outside on the road. Similarly, the northern point is almost 200m from the nearest part of the big road (I-80?) which is as close as Mapbox's routing system can calculate. GoogleEarth is able to run a route much closer.

The distance you want should be available by using some of the optional parameters in your API call and probably the Route Leg Object.

like image 40
Magnas Avatar answered May 30 '26 09:05

Magnas