HERE Maps API路由与Toll Cost服务同参数返回不同路线问题求助
It looks like you're hitting a common gotcha with HERE Maps API: even when using identical parameters, the Route Service and Toll Cost Service can return different routes because their underlying calculation logics are optimized for distinct goals. Here's how to align them to meet your requirement of prioritizing toll-free routes, only falling back to toll roads when no alternatives exist:
1. Understand the Parameter Conflict
Your current setup uses cost_optimize=1 alongside tollroad:-2, but these two parameters can clash:
cost_optimize=1in the Route Service optimizes for overall trip cost (combining fuel, time, and tolls), which might prioritize a shorter toll road if it reduces total trip cost—even if you want to avoid tolls.tollroad:-2tells the service to avoid toll roads unless no alternative exists, but the cost optimization logic can override this priority.
The Toll Cost Service, by contrast, focuses specifically on toll-related costs, so it’s more likely to surface toll road routes when they’re the most cost-efficient from a toll-only perspective.
2. Adjust Route Service Parameters to Prioritize Toll Avoidance
Modify your Route Service request to explicitly prioritize toll avoidance over general cost optimization:
Updated Route Request URL
https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apikey=&cost_optimize=0&tollVehicleType=3&vehicleNumberAxles=3&trailerNumberAxles=2&waypoint0=39.72793,-105.01245&waypoint1=41.09575,-104.84611&routeAttributes=sm,t&mode=fastest;truck;traffic:disabled;tollroad:-2
Key changes:
- Set
cost_optimize=0to disable general cost optimization, ensuringtollroad:-2takes top priority. - Add
ttorouteAttributesto include detailed toll information in the response—this lets you verify if any segments of the route use toll roads, even if the summary flags don’t list "tollroad" (summary flags only highlight major route features).
3. Verify Toll Road Strictness
If the above adjustment still returns a route without toll roads but the Toll Cost Service shows a toll route, test with tollroad:-3 (strictly avoid toll roads):
- If this returns a "no route found" error, that confirms there are no fully toll-free routes between your waypoints. Your original
tollroad:-2route is the only feasible option, and the summary flags might not be reflecting minor toll segments correctly. - If it returns a valid route, then your original
tollroad:-2request wasn’t working as expected, and settingcost_optimize=0should fix the discrepancy.
4. Align Toll Cost Service Parameters
To ensure consistency across both services, update your Toll Cost Service request to match the adjusted Route Service parameters (set cost_optimize=0):
Updated Toll Cost Request URL
https://tce.api.here.com/2/calculateroute.json?app_id=&app_code=&cost_optimize=0&mode=fastest;truck;traffic:disabled;tollroad:-2¤cy=USD&tollVehicleType=3&vehicleNumberAxles=3&trailerNumberAxles=2&representation=overview&waypoint0=39.72793,-105.01245&waypoint1=41.09575,-104.84611&routeAttributes=sm
5. Reach Out to HERE Support
If you still see discrepancies after aligning parameters, the issue might stem from differences in route databases or calculation engines between the two services. Submit a support ticket with your full request/response details, and HERE’s team can investigate the underlying logic mismatch.
内容的提问来源于stack exchange,提问作者Laimonas Moncius

