如何通过HERE API获取各途经点之间的距离?
Hey there! Let's break down how to grab the exact distances for your two route segments (waypoint0 → waypoint1 and waypoint1 → waypoint2) using the HERE Route API.
Your current request is already close — you've included sm (summary data) in routeAttributes, which is key. The main tweak you need is to adjust the legAttributes parameter to get per-segment details instead of just total route info.
Updated Request
Change legAttributes=none to legAttributes=li,sm (this enables leg information and leg-specific summaries). Here's your modified request string:
https://tce.cit.api.here.com/2/calculateroute.json?mode=fastest;truck;traffic:disabled&truckType=tractorTruck&maneuverAttributes=none&combineChange=true&routeAttributes=none,wp,sm,sh,no&linkAttributes=none&legAttributes=li,sm&detail=1&alternatives=2&rollup=total,country&jsonattributes=41&waypoint0=geo!stopOver!53.55155000,9.99960000&waypoint1=geo!passThrough!45.97279231,4.73305852&waypoint2=geo!stopOver!41.15305000,-8.57573000
What This Changes
- With
legAttributes=li,sm, the API will return alegsarray in the JSON response. Each entry in this array corresponds to one segment between your waypoints. - Inside each leg object, you'll find a
summaryfield that includes thedistance(in meters by default) for that specific segment, along with travel time and other relevant metrics.
Example Response Snippet
Here's a simplified look at what you'll get for the legs section:
"legs": [ { "summary": { "distance": 895678, "travelTime": 37260 // Additional summary details will appear here } }, { "summary": { "distance": 789123, "travelTime": 32100 // Additional summary details will appear here } } ]
The first leg's distance is for waypoint0 → waypoint1, and the second is for waypoint1 → waypoint2.
A quick note: since waypoint1 is marked as passThrough, the API still recognizes it as a split point for legs when you enable legAttributes=li,sm, so you'll get the exact segment distances you need.
Hope this solves your problem — let me know if you need further clarification! 😊
内容的提问来源于stack exchange,提问作者cslehel

