Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API - detect when crossing state lines and calculate distance travelled in each state

I am using google maps api to calculate the number of miles my company's vehicles travel in each state over a period of time. For trips taken over the period, the user will create routes on the map, and the program should be able to generate some output like: 'Maryland: 100 miles, Virginia: 500 miles", etc.

I'm pretty sure I can do it by sending a DirectionsService request and iterating over the response.routes[i].legs[i].steps[i].path array, but unfortunately this requires sending Geocoder requests on thousands of LatLngs, which quickly puts me over my quota. Is there a better way to go about this? I really only need to detect when the route crosses a state line and break it into separate routes, but I don't know how I would do that without iterating over the path array.

like image 702
grandinero Avatar asked Oct 03 '22 13:10

grandinero


2 Answers

If you have 1000 elements in your path array and you are binary searching (instead of iterating), it should only take 12 calls to LatLng to determine where the path crosses the border.

What sort of feedback do you get when you go over quota? If it's an exception return, you could always do the 3.5 second wait only when you receive the quota pushback.

like image 66
Sniggerfardimungus Avatar answered Oct 07 '22 20:10

Sniggerfardimungus


Looking at the directions text in the panel, it looks like the text "Entering " appears in the steps that cross state boundaries.
http://www.geocodezip.com/v3_example_geo2.asp?addr1=New%20York,NY&addr2=Washington,%20DC&geocode=1&geocode=2

  • Entering New Jersey
  • Entering Delaware
  • Entering Maryland
  • Entering District of Columbia

(this is not documented, so might stop working)

You could also find the intersection of the Directions polyline with a set of state polygons.

like image 28
geocodezip Avatar answered Oct 07 '22 18:10

geocodezip