Distance matrix api for Google - google-maps

I am using Google Distance Matrix API. But I am unable to figure out the API cost, it has written on their website that "price per element". So, if I have two origins and 1000 destinations, then what will be my total API cost, and is the example program with Google OR tools is enough to run 1000 destination distance API call and solve the matrix?
Please Help!!

Each Distance Matrix API call generates a number of elements (the number of origins times the number of destinations, e.g. 10 origins * 10 destinations = 100 elements) and each element costs $0.005 as per Google's documentation. So in the provided example, you'd be billed 0.005 * 100 = $0.5 per call to the Distance Matrix API.
Also, note that you cannot add two origins and 1000 destinations to a single request. You are limited to a maximum of 25 origins or 25 destinations per request, hence in order to query 1,000 destinations you'd need to make multiple Distance Matrix API requests.
Hope this helps!

Related

Filter POIs that are close to a route

I have a list of Points-of-Interest (e.g. car rest areas).
The user selects the Starting Point and the Ending Point.
This generates a route.
How can I programmatically filter the POIs that are close (e.g. 50 meters distance from the road) that route?
Can Google Maps SDK or OSRM offer this functionality?
Thank you,
Nick
1. You have to find the distance from one POI to the road.
In order to accomplish this, you have to store your road in a mathematical fashion:
You can sample equidistant points of your road and store them in an array (more practical, less precise) and then calculate the distance of the POI from every point in the array, then save the minor result and repeat the whole process for every POIs.
You can store a road in a function (more and more complex, but more precise). Now that you have this function, you can calculate same distance from your POI, take the minimum value and repeat for all POIs.
2. Google Distance Matrix can actually do this
With this Api you can calculate distance till 2500 origins * destinations points.
The result will give you an array of rows, with each row corresponding
to an origin, and each element within that row corresponds to a pairing of the origin with a destination value.
Example of a request:
https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=32.777211,35.021250&destinations=32.778663,35.015757&key=YOURAPIKEY
This is very useful to your goal, because lets you specify more than one points of which calculates distance.

If you make a single googlemaps API request with multiple origin/dest addresses, does that count as a single request?

Since there is a free quota of 2500, I am wondering if there's anything I could do to optimize the number of requests I make to the API.
If I make a single request with 1 origin address & 2 destination addresses, does that count as 2 requests in terms of the quota?
Thank you
The answer to your question is NO, In Distance Matrix API you have a usage limit of 2500 free elements per day (Standard Plan)[2].
where the:
Nº of Elements = Nº Origins x Nº Destinations [1]
and you can have:
A maximum of 25 origins or 25 destinations per request.
A Maximum 100 elements per request.
A Maximum 100 elements per second*, calculated as the sum of client-side and server-side queries.[2]
[1] https://developers.google.com/maps/faq#usage_quotacalc
[2] https://developers.google.com/maps/documentation/javascript/distancematrix#UsageLimits

Google Maps Distance Matrix API request per day

I can call Google Maps Distance Matrix API in two ways:-
1) with single origins zip code and single destinations zip code
eg: https://maps.googleapis.com/maps/api/distancematrix/json?origins=48084&destinations=48326&key=" + API_KEY
2) with array of origins zip code and array destinations zip code
eg : https://maps.googleapis.com/maps/api/distancematrix/json?origins=48084%7C48098%7C48309&destinations=48326%7C48306&key=" + API_KEY
1) How the request/day is counted in case 1 and case 2?
2) Can I save the distance and time locally in the cache of DB for performance benefit ?
Case1: Here you are using single origin and single destination.So,
If you are want multiple conversions then you need to hit
this api multiple times which incurs multiple request.
origin destination #Request
A B 1
A C 1
-------------------------------------------------------------
Total Request 2
Case2: In this case you can achieve multiple conversions with a single
api hit,means its just single request.
orgin destination #Request
A B,C 1
--------------------------------------------------------------
Total Request 1
Conclusion: Case2 is less expensive in terms of no of api request

Google API - Place Search - RankBy Distance

As per the google api documentation its mentioned as:
"rank results strictly by distance. In order to rank results by distance you must use some form of query or filter on the search. This can be a name filter, a type filter, or a keyword search. When results are ranked by distance it is not necessary to provide a search radius as the Places API will try to return the 20 closest results within reasonable distance"
Reference:
https://maps-apis.googleblog.com/2012/05/google-places-api-search-refinements-as.html
My question here is, what's the maximum radius/distance the API uses internally to get the matching results?
Note: For radius, the api documentation says "The maximum allowed radius is 50 000 meters". But no information about maximum/default distance the result includes is mentioned for rankBy: google.maps.places.RankBy.DISTANCE.
Any information on this will be helpful.
According to Google team the radius used with rank by distance option is 7000 meters.
Take a look at the following answer in the public issue tracker:
https://issuetracker.google.com/issues/35824648#comment2
Indeed, rankby=distance uses a default radius, currently 7000 m, and only results within that radius will be returned.

Using google maps API to find average speed at a location

I am trying to get the current traffic conditions at a particular location. The GTrafficOverlay object mentioned here only provides an overlay on an existing map.
Does anyone know how I can get this data from Google using their API?
It is only theorical, but there is perhaps a way to extract those data using the distancematrix api.
Method
1)
Make a topological road network, with node and edge, something like this:
Each edge will have four attributes: [EDGE_NUMBER;EDGE_SPEED;EDGE_TIME,EDGE_LENGTH]
You can use the openstreetmap data to create this network.
At the begining each edge will have the same road speed, for example 50km/h.
You need to use only the drivelink and delete the other edges. Take also into account that some roads are oneway.
2)
Randomly chose two nodes that are not closer than 5 or 10km
Use the dijsktra shortest path algorithm to calculate the shortest path between this two nodes (the cost = EDGE_TIME). Use your topological network to do that. The output will look like:
NODE = [NODE_23,NODE_44] PATH = [EDGE_3,EDGE_130,EDGE_49,EDGE_39]
Calculate the time needed to drive between the two nodes with the distance matrix api.
Preallocate a matrix A of size N X number_of_edge filled with zero value
Preallocate a matrix B of size 1 X number_of_edge filled with zero value
In the first row of matrix A fill each column (corresponding to each edge) with the length of the edge if the corresponding edge is in the path.
[col_1,col_2,col_3,...,col_39,...,col_49,...,col_130]
[0, 0, len_3,...,len_39,...,len_49,...,len_130] %row 1
In the first row of matrix B put the time calculated with the distance matrix api.
Then select two news node that were not used in the first path and repeat the operation until that there is no node left. (so you will fill the row 2, the row 3...)
Now you can solve the linear equation system: Ax = B where speed = 1/x
Assign the new calculated speed to each edge.
3)
Iterate the point 2) until your calculated speed start to converge.
Comment
I'm not sure that the calculated speed will converge, it will be interesting to test the method.I will try to do that if I got some time.
The distance matrix api don't provide a traveling time more precise than 1 minute, that's why the distance between the pair of node need to be at least 5 or 10 or more km.
Also this method fails to respect the Google's terms of service.
Google does not make available public API for this data.
Yahoo has a feed (example) with traffic conditions -- construction, accidents, and such. A write-up on how to access it is here.
If you want actual road speeds, you will probably need to work with a commercial provider.