views:

46

answers:

2

I've got a page that has a map with a starting and ending location. I run a route between them to get the nifty line showing the route. I'm currently using Bing but have attempted with Google as well. I'd like to know which states this route passes through so I can then overlay those states with specific information.

Any suggestions on how to obtain this would be most appreciated.

I'm using the AJAX SDK's for both Bing and Google. Handling all the local stuff with js/jquery.

+1  A: 

You can use a reverse geo-code request on the Google Maps API to determine what state a particular point is in. So I imagine you could process your array of points returned with the directions request and pull out the state for each one.

In the v2 API, this would be:

results.AddressDetails.AdministrativeArea.AdministrativeAreaName

I think this is a bit more intuitive in v3. You can examine the AddressComponents array in the results to find the appropriate type:

{
    "long_name":"California",
    "short_name":"CA",
    "types":["administrative_area_level_1","political"]
}

You could optimize the reverse geocoding by using divide and conquer on the array of positions on the route (if the state is the same for the first and middle position, then don't do reverse geocoding on the intervening points).

Cannonade
There may well be more efficient ways of doing this (a custom service perhaps?) and I would be interested in seeing alternatives. But this was my first reaction to the problem.
Cannonade
+1: The divide and conquer is a very good idea
Daniel Vassallo
@daniel-vassallo Thanks Daniel.
Cannonade
I have not tried with google yet but this does in fact work, to a point with Bing and I'm assuming a similar result with Google would be the case. I reverse geocoded all the "legs" of the route but in some cases there were no legs in a given state on the route so therefore when I loaded all the respective overlays for the states found, some were missing. Your answer was sufficent enough to get me moving in the right direction. Other suggestion would be welcome.
Bert Smith
A: 

You could create your own service utilizing a shapefile and a library like SharpMap or a geodatabase like mysql spatial, sql server spatial, etc. Then you simply just need to run an intersection query to discover which states your route runs through. This approach would work for any polygon set, so you could easily extend the solution to counties, voting district, school districts, etc.

Peter Smith