문제

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.

도움이 되었습니까?

해결책

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).

다른 팁

The MapQuest Directions web service has a stateBoundaryDisplay flag that will explicitly put state boundary crossings (ie "Crossing into statename") in the narrative. You could easily pull just that info from the json/xml response with a text search of the narrative steps.

http://www.mapquestapi.com/directions/#advancedoptions

Hope that helps.

Roman

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top