最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

qt - Getting total duration and distance on MapBox JavaScript - Stack Overflow

programmeradmin1浏览0评论

I'm using MapBox GL API for Qt (JavaScript), and I have to get the total distance and total duration from a route. To obtain the route, I'm making the following http request:

.61767353533753,42.102184359031504;-8.495246688170312,41.44289308091726?steps=true&geometries=geojson&access_token=myToken

The response is in geoJSON format, which I use to draw the route on my map, and it works.

The problem is I can't calculate the total distance and duration of the whole trip. There are many "duration" and "distance" fields in this geoJSON.

Do I have to work with these multiple fields, or is there any other way to calculate the total distance and duration?

I'm using MapBox GL API for Qt (JavaScript), and I have to get the total distance and total duration from a route. To obtain the route, I'm making the following http request:

https://api.mapbox./directions/v5/mapbox/driving/-8.61767353533753,42.102184359031504;-8.495246688170312,41.44289308091726?steps=true&geometries=geojson&access_token=myToken

The response is in geoJSON format, which I use to draw the route on my map, and it works.

The problem is I can't calculate the total distance and duration of the whole trip. There are many "duration" and "distance" fields in this geoJSON.

Do I have to work with these multiple fields, or is there any other way to calculate the total distance and duration?

Share Improve this question edited Jul 27, 2020 at 17:56 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Apr 23, 2020 at 10:44 HMI SWHMI SW 531 silver badge5 bronze badges 2
  • Just as tip, you are sharing here the API url but this contains your access token too. – leonardfactory Commented Apr 23, 2020 at 11:35
  • @leonardfactory Thanks for the tip! I solved my problem following the Steve Bennett indications. Thank you too for your help! – HMI SW Commented Apr 27, 2020 at 8:38
Add a ment  | 

2 Answers 2

Reset to default 4

To get the distance and duration, just take routes[0].distance and routes[0].duration respectively.

(You don't want to add them up: these represent alternative routes, not parts of one long journey.)

See https://docs.mapbox./api/navigation/#directions-response-object .

You can use the turf.js library passing it the geoJSON and then do some spacial analysis.

The concept here is to pass a geoJSON feature to turf and use the length function:

var geojson = /** get your shape from API */;
var length = turf.length(geojson, {units: 'kilometers'});

Here is an example directly from MapBox: https://docs.mapbox./mapbox-gl-js/example/measure/

Edit: since there is a duration and distance in the routes specified by mapbox api, you can process the geojson.routes object, like this:

var duration = geojson.routes.reduce(
 (d, route) => d + route.duration
 0
);

This way you can even use the route.distance field in order the get the total distance.

EDIT n.2: DON'T DO THIS! I messed it up, routes is just an array of alternatives, kudos to Steve Bennet for pointing this out.

发布评论

评论列表(0)

  1. 暂无评论