Generate list of states route passes through

13 posts / 0 new
Last post
Generate list of states route passes through

I need to generate a list of states the my route passes through. Is there any default functionality that will do this or a recommended approach?


E.g I have a route that goes through 6 states. I need to generate a list including all of those states.

There is no function that
There is no function that returns a list of states for a route. But if the stateBoundaryDisplay parameter is set to true, the response will contain details like, "Crossing into Nebraska", in the maneuver text.

Thank you for both replies.

Thank you for both replies. Here and Stack.

Do you offhand know of a way to obtain the distance traveled in each state?

States routed through Colorado, Nebraska.
Colorado: 123.5miles, Nebraska 567.2 miles

Distances in the route
Distances in the route response are per maneuver. There is no simple method I'm aware of to get the distance per state.

multi request approach

I'm thinking if I can retrieve a lat/long for each state boundary I can then build separate state routes.Basically break the full route down in to pieces. Use the stateBoundaries as start/finish for each state. What do you think?

If you have the state bounds
If you have the state bounds then you could also find the closest shape points in the route response and use the shape points in that state to calculate the distance. It's a bunch of client side math so if it's javascript in a browser it may slow things down a bit but if it's server side it should not be bad.

90% will be done server side

90% will be done server side with Php.

Form (A-B addresses) -> Processed via Php, Curl (post) request fastest route

Display Map, Drag route

Confirm changes -> Php process -> Curl (post) request directions

Php process state mileage

Display static map and data.

update -> working

Form (Startpoint address, Endpoint address...various other data) -> Php.
Php inserts form data to DB -> outputs page -> MQ API displays route (leaflet).
Client Drags new route -> JS passes Data.Route "string" [JSON.stringify(data.route)] to form -> Client submits final route form.
Php parses form -> Loops through data.route and compiles total mileage per state (data.route.legs.0.maneuvers -> narrative,distance)
Mileage, data.route etc updated in db record -> display state based mileage and various other bits of data.

No additional calls to the API are required. The manuevers->distance value is used alongside the manuevers->narrative "Crossing into" substring to determine stop/start points for new state boundaries. The accuracy of the state mileage is based on the assumption that the execution of the "Crossing into" manuever will put you in the new state.

Simply put the manuever states: (Crossing into Ohio) and the distance for that manuever is 4.89 miles. At the end of that 4.89 miles, you are infact in Ohio.


I will be posting this information and code on StackOverflow. If the mods have no objection I will post a link in this thread once I get it on SO.

Links to StackOverFlow
No objections.  Thank you for sharing the details of your solution with the developer community.

Where is the link to his code

Where is the link to his code, I would love to examine to see I can find anything useful that can be applied to my application.

I don't see it up there yet.
I don't see it up there yet. But here is the link to the topic.


Sorry everyone for the late reply.

I ran into a snag with the "stateBoundaryDisplay" and pushed it to the back burner.

Basically the problem is on some routes the Directions data does not include a "Crossing into" string for each state indepently. e.g. (Crossing into Iowa). It will combine multiple states in a single Crossing string. 

The problem route Was Oregon to Ohio. (Oregon > Idaho > Utah > Wyoming > Nebraska > Iowa > Illinois > Indiana > Ohio)

So the first state for mileage calculation is Oregon (start point). The first "crossing into" string would stipulate the end of mileage calculation for Oregon (crossing into Idaho) and the start of the next state mileage calculation (Idaho).

The directions passed the following boundaries:

  • (Passing through Idaho, then crossing into Utah)... Oregon mileage included Idaho.
  • (Passing through Wyoming and Nebraska, then crossing into Iowa)... Utah mileage included Wyoming and Nebraska.
  • (Crossing into Illinois)
  • (Crossing into Indiana)
  • (Crossing into Ohio)

Until a "Crossing into" string is passed for each stateBoundary this approach is pretty much dead in the water.

Returning distance in each
Returning distance in each state in the route response has been forwarded on to the route team as an enhancement request. This will be reviewed and maybe included in a future version. Thanks for the feedback.