Google Maps API - Detect when crossing state lines and calculate distance traveled in each state

I use the google maps api to calculate the number of miles that my company vehicles travel in each state for a certain period of time. For trips undertaken during the period, the user will create routes on the map, and the program should be able to generate some result, for example: "Maryland: 100 miles, Virginia: 500 miles", etc.

I'm sure I can do this by sending a DirectionsService request and iterating over the response.routes[i].legs[i].steps[i].path , but unfortunately this requires sending Geocoder requests to thousands of LatLng s, which my quota quickly transfers me. Is there a better way to do this? I really only need to detect when the route crosses the status line and breaks it into separate routes, but I do not know how to do this without repeating the path array.

+4
google-maps-api-3
source share
3 answers

If you have 1000 elements in your array of paths and you perform a binary search (instead of iteration), you need only 12 calls in LatLng to determine the border path.

What feedback do you get when you go over the quota? If it returns an exception, you can always execute 3.5 seconds of waiting only when you get a quota rollback.

+1
source share

When you look at the direction text in the panel, it seems that at the steps crossing the state boundaries the text β€œEnter” appears.
http://www.geocodezip.com/v3_example_geo2.asp?addr1=New%20York,NY&addr2=Washington,%20DC&geocode=1&geocode=2

  • Entry to New Jersey.
  • Entering Delaware
  • Entrance to Maryland
  • DC Entrance

(this is not documented, so it may stop working)

You can also find the intersection of the Directions polyline with many state polygons.

+1
source share

Personally, I used GeoNames for something like this, and not for Google.

With Geonames, you are likely to run into quota problems like Google, but there is a big difference ... Geonames allows you to upload different datasets so you can provide your own self-service geoservices ... without a quota.

I never had to do this, so you can advise you to jerk on the Geonames website for instructions.

Basically you need to:

  • Download the US.zip dataset and install on the server (or localhost for development)
  • Write a specialized (RESTful) webservice (for example, in PHP) that performs all transitions crossing the state line and returns a list of spaces in each state in JSON.

Performing a tough mathematical server part and returning the related results, you will need to make only one HTTP request (per company car).

Of course, the server will need the appropriate lat / lng path data, but from what you say in the question, which is already server-side, so there is no need to download it to the client to be resent in the request,

An interesting project. From the very beginning, you will need to think about your testing / verification strategy.

Sorry, I can’t help anymore, but maybe this gives you a way forward.

+1
source share

All Articles