Google Maps API Tutorial

© 2006, 2007 Mike Williams

 

State Mileage

Some companies require mileage reports to list the mileage in each US state.

The information in a GStep now mentions when the route crosses a US state boundary, so it is now possible to break down a GDirection mileage by state.

However, that information only tells us the state change point to the nearest GStep, and there's a possibility that a GStep might be hundreds of miles long. That's probably not very useful.

So what I do is chop that step in half and get GDirections for the two parts to see which half contains the state boundary. The halving process is repeated until the length of the part becomes less than some specified accuracy value.

Here's an example

The first state name is obtained by using GDirections.getGeocode() which gives the state code.
Subsequent state names are obtained from the GStep information, which gibes the full state name.

WARNING

The code depends on the layout of the GSteps information. Google may change this at any time, causing the technique to stop working.

WARNING

This process uses a significant number of GDirection requests, and may cause you to exceed your quota. Unlike GClientGeocoder, when you exceed your GDirections quota you don't seem to get a 620 error, you get no notification at all. If you use a smaller value for "accuracy" then you will use more GDirection requests and reach your quota sooner.

Back to Mike's Homepage
Up to the start of this tutorial