I have a database with a list of locations and I have to get the route steps, drive distance and drive time for all combinations of locations in the database.
I have the latitude and longitude stored in the database for every location.
I'm using the google directions service api to get the information.
As long as the number of records was low, I could get the results from my code within a minute using threading.
However as the number of records increased, the number of records in combination increased exponentially (trust me, this is not the issue and the number of combinations have the potential to reach 100K), thereby causing an increase in the number of requests to be made to directions api and hence the time taken for retreival.
I understand since a large number of rest connections need to be made to the directions api, network latency plays in and the time taken to retrieve the results increases.
I googled and found that some of the maps api's (for instance waypoints in the directions service api) allow you to specify more than 1 value using the pipe operator ( | ).
I attempted to do the same and retrieve information for one source multiple destinations for every rest request however I get a NOT_FOUND status returned from the directions service.
Could someone please help me figure out how to get routes and the distance and time from the same origin but multiple destinations using the directions api.
Following is a sample url and the response I received (API_KEY was removed on purpose, cannot share that):
https://maps.googleapis.com/maps/api/directions/json?key=API_KEY&origin=49.794150,-84.564514&destination=49.244978,-88.146057|49.173205,-84.756775
{
"geocoded_waypoints" : [
{},
{
"geocoder_status" : "ZERO_RESULTS"
}
],
"routes" : [],
"status" : "NOT_FOUND"
}
Directions API doesn't support multiple origins or destinations. You can specify one origin, one destination and up to 23 waypoints.
https://developers.google.com/maps/documentation/directions/intro#Waypoints
The request with waypoints looks like:
https://maps.googleapis.com/maps/api/directions/json?origin=49.794150%2C-84.564514&destination=49.173205%2C-84.756775&waypoints=49.244978%2C-88.146057&key=YOUR_API_KEY
If you would like to test your routes and see the visual representation, you can use Directions calculator:
https://directionsdebug.firebaseapp.com/?origin=49.794150%2C-84.564514&destination=49.173205%2C-84.756775&waypoints=49.244978%2C-88.146057
Hope it helps!
Related
I'm trying to see if there's a way in a single API call to find the ideal route, order not mattering, between X destinations.
For example, the program has 3 destinations, Jeff's house, Amy's house, and Valerie's house. Don't really care the order we go in, but we'd like to visit each house with the least amount of driving.
Right now, I have it set up such that we try every ordering of destinations, and settle on the one with the fastest time. But having so many API calls seems inefficient, but I can't see a way in the API to do what I want. Is what I want presently possible in the google maps API?
You can use Waypoints in Directions API web service which returns a route that includes pass throughs or stopovers at intermediate locations.
By default, the Directions service calculates a route through the
provided waypoints in their given order. Optionally, you may pass
optimize:true as the first argument within the waypoints parameter to
allow the Directions service to optimize the provided route by
rearranging the waypoints in a more efficient order.
Sample request:
https://maps.googleapis.com/maps/api/directions/json?
origin=Adelaide,SA&destination=Adelaide,SA
&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA
&key=YOUR_API_KEY
Note that requests using waypoint optimization are billed at a higher rate.
If you will be using client-side Maps JavaScript Directions Service. Refer to this documentation and example.
Hope this helps!
I'm using the google maps distance matrix javascript API to query the travel time from one point to about 50 destinations.
in order to do this, I need to break my query up into multiple chunks, since the google maps API allows only a maximum of 25 destinations per request.
the problem that I'm having is the synchronization of the request and the result.
other APIs offer the option to include a key that lets the developer match a request and an asynchronous result.
however, since such a parameter is missing from the request specification, I have no idea how to make sure that I'm matching the right data to my incoming results.
I would be glad for a "clean" workaround - currently the only idea that I'm having is to have a unique number of destinations for each request, for instance the first with 25, the next with 24 destinations and so on. but I would not consider this a satisfactory approach.
thank you
I'm using Google Maps web-services -> Distance Matrix API.
My goal is getting driving time between two or more fixed locations, but considering real-time traffic.
I have tried a couple of parameters combinations, but I always get a static trip duration on the replied JSON.
Do you have a recommendation regarding building a request for getting back tip duration considering real-time traffic?
I have tried the following request on a browser, in order to get a JSON reply:
http://maps.googleapis.com/maps/api/distancematrix/json?&origins=Guadalajara&destinations=Chapala&mode=driving&departure_time=now
However, the parameter "duration" is always the same:
duration
text "54 mins"
I would like to get the trip duration considering real time traffic as part of the replied JSON, like when using Google Maps on web:
https://www.google.com/maps/dir/Guadalajara,+Jal./Chapala,+Jal./#20.4809265,-103.3652921,11z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x8428b18cb52fd39b:0xd63d9302bf865750!2m2!1d-103.3496092!2d20.6596988!1m5!1m1!1s0x842f40b1771f4cb7:0x73c1721b67d87f53!2m2!1d-103.1846016!2d20.3051576!3e0
I have a number of target points on the map and a source location. I need to find the target point closest to the source location. The trick here is that "closest" means the shortest route. I can't use a simple crow fly distance.
This is essentially a one-to-many routing problem. I can get the answer by running a routing API call for each pair of points but that would be too slow and will blow up API usage.
Is there a way to do it with a single request using Google Maps API (I am ok paying for it if necessary)?
If yes, then what are the limits to the number of points, the request frequency, etc?
If no, is there another service that can do it?
Say I have a lot of points (thousands). Is there a way to upload them somehow and only use the source location in my requests?
You should take a look to Distance Matrix Service
You can specify a starting point and an end point (even intermediate waypoints), then get the distance in kilometers / miles by route.
To answer your questions :
Is there a way to do it with a single request using Google Maps API (I am ok paying for it if necessary)
Yes you can, just use the service given below
If yes, then what are the limits to the number of points, the request frequency, etc?
To quote the Usage limits and requirements :
The following usage limits are in place for the Distance Matrix service:
Maximum of 25 origins or 25 destinations per request; and
At most 100 elements (origins times destinations) per request.
Requests are also rate limited. If too many elements are requested within a certain time period, an OVER_QUERY_LIMIT response code will be returned.
Say I have a lot of points (thousands). Is there a way to upload them somehow and only use the source location in my requests?
I'm not sure what you really want, but you can define a point as "source location", then load it (from SQL, KML etc...), then load the targets and call the service to find which one is the closest.
I am now starting new project and I wanted to know if I could use google maps to solve my problem and get list of countries in selected route.
I tried to search on internet about this but couln't find anything what I needed.
Edit: So I created function which loops through routes steps and by given latitude/longitude coordinates in each step I can get in which country is this step taken by using googles geocoder.geocode function. But now I face new problem while I am looping through steps I get this error: OVER_QUERY_LIMIT indicates the webpage has sent too many requests within the allowed time period.
I even tried to set timeout for 500ms before caling geocode function but no use.
If you do a route request, it will always have details about crossing international boundaries.
Just try doing a from/to request on maps.google.com and you will see in the directions, when it crosses a border.
That same information is available to us to take from the direction response.