5

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.

grandinero
  • 1,155
  • 11
  • 18
  • 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. Are you looking for a solution that involves minimizing the number of items in the path array that you have to check, or are you looking for a solution, within the API, that will find the border for you? – Sniggerfardimungus Jul 15 '13 at 18:03
  • A simple test route generated over 900 LatLng objects, and the server only responded to four requests. A route that goes through several states could easily generate tens of thousands of LatLngs; I don't think there's any way to sufficiently reduce the request load. – grandinero Jul 15 '13 at 22:44
  • Actually, I was able to get it to work by combining your binary search suggestion with a 3.5 second timeout (an eternity, but I'll take what I can get right now). Post it as an answer and I'll accept it. – grandinero Jul 19 '13 at 04:55
  • possible duplicate of [Google maps api v3 calculate mileage by state](http://stackoverflow.com/questions/34028829/google-maps-api-v3-calculate-mileage-by-state) – geocodezip Mar 19 '16 at 00:52

3 Answers3

2

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.

Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97
  • It is an exception return, but the quota is hit so soon and so frequently that I prefer to space the queries out for a steady, consistent flow. I've reduced the delay to 3 seconds, with an additional 3 second timeout when the quota is reached before trying again. Compared to the other time combinations I've tried, this is the most bearable. But your binary search suggestion was key-- works like a charm. Thanks! – grandinero Jul 20 '13 at 01:48
1

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.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Now that you mention it, state polygons are probably the way to go. However, they will be tedious to set up, so in the meantime I will have to make the user manually place a waypoint where the route crosses a state line. – grandinero Jul 15 '13 at 22:46
1

Personally, I would use GeoNames for something like this, not Google.

With Geonames, you will most likely run into quota problems as with Google, however, there's a big difference ... Geonames allows you to download various datasets so you can provide your own self-hosted geo-service(s) ... sans quota.

I've never needed to do this so can only advise you to rummage round in the Geonames site for instructions.

In principle, you need to :

  • Download the US.zip dataset and install on a server (or localhost for development)
  • Write a specialized (RESTful) webservice (eg. in PHP) which does all the state line crossing calcs and returns a JSON-encoded list of mileages per state.

By doing the hard math server-side and returning bundled results, you will only need to make one HTTP request (per company vehicle).

Of course, the server will need the appropriate lat/lng path data but from what you say in the question, that's already server-side, so no need for it to be downloaded to the client just to be resent in the request.

Interesting project. From the outset, you will need to think about your testing/verification strategy.

Sorry I can't be of more help but maybe this gives you a way ahead.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44