How to request Waypoints in Google Map Distance Matrix API? - google-maps

I want to use Google Map Distance Matrix API but I also want to include Waypoints in it.
Distance matrix take 2 inputs only as it takes list of Starting Points and list of Destinations points. I want to include Starting Point, Destination Point and Final Destination Point.
So it should show distance matrix of A -> B -> C
How can I incorporate that ?

Use A and B as origins.
Use B and C as destinations.
Your response should contain A->B, A->C, B->B, B->C distances.
Extract & sum A->B and B->C to receive your target distance with a single API call.

Related

Distance matrix api for Google

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!

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.

using rethinkdb calculate distance between two latitude and longitude points

we are using rethinkdb geospatial features to calculate distance between two latitude and longitude but the result returned by rethinkdb is different and looks wrong if i cross check on google maps or any distance calculator website. I have copied same code given rethinkdb help.
var point1 = r.point(-122.423246,37.779388);
var point2 = r.point(-117.220406,32.719464);
r.distance(point1, point2, {unit: 'km'})
// result returned
734.1252496021841 km
but when i test same point on http://www.onlineconversion.com/map_greatcircle_distance.htm it return following result 642.1854781517294 km.
Different from some other geo systems, RethinkDB uses the convention of having the longitude first, followed by the latitude.
We made that decision in order for being consistent with the GeoJSON format.
See http://www.rethinkdb.com/api/javascript/point/
From looking at your example, it looks like you've computed the distance correctly in RethinkDB, but entered the coordinates in the opposite direction in the online tool.
With latitude and longitude entered into the correct fields, I'm getting consistent results:
A more advanced note:
There is some difference behind the decimal point. The online calculator claims that "The script uses "Haversine" formula, which results in in approximations less than 1%." by which I assume it means up to 1% error, so this sort of deviation is to be expected.
RethinkDB uses geodesics on an ellipsoid for computing distances, based on the algorithm by C. F. F. Karney 1. This is an extremely precise algorithm, that calculates geodesics up to the limits of double-precision floating point numbers.
You will see even more deviation from Google maps (it gives me 735.234653 km for these two points). It looks like Google maps uses great-circle distances, which do not take the ellipsoidal shape of the earth into account at all.
1 http://link.springer.com/article/10.1007%2Fs00190-012-0578-z

google map placing locations with some distance

I'm new to ROR and Google Maps. I need to place some markers from locations in Google Maps (having latitudes and longitudes in a database).
The problem is that I need to select some points with some random distance.
In short, I need to select the location and place it in a map, which must have 100 m distance with each and every points.
If the location is within 100 m range with any other points, it can be neglected. I need to place 10 points from database.
Is there any method?
Assuming that you are needing to find points from your database that are at least 100 meters away from all the other points in the database:
This is a fairly simple problem. It can be visualized as an nxn matrix, with the point set as the rows and columns. In Python, comparing all the distances would look like:
selected = []
for pt1 in pts:
inRange = True
for pt2 in pts:
if pt1.distanceTo(pt2) < 100:
inRange = False
break
if inRange:
selected.append(pt1)
This function iterates through the whole list of points. For each point, it checks the distance from the current point to all the other points. If all the other points are outside 100 meters, it adds the point to an array.
For the distance formula, please see the haversine formula here in code form.
Since you did not specify a language in your question, I will let you translate this into whatever language you need. This is just pseudocode, since not enough details were provided to answer your question with actual code.
Also, if I misunderstood your question, you can adapt this algorithm in some way. It is just to provide some ideas.

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.