Google Map Distance Calculation - google-maps

Does anyone know if Google Maps consider elevations and the actual path on earth to calculate distance between two points or it only considers geographical locations for distance measurement?

If you are referring to using google.maps.DistanceMatrixRequest then yes, it considers the actual route when calculating distance, based on the option provided in travelMode
code.google.com reference
When there is no established route (at least known to google) i'm sure (from experience) the response is the distance 'as the crow flies' between 2 lat/long locations. [experience based off making the request for a path that bisected a large lake]

Related

Can Google Maps API calculate route along multiple locations or journey legs?

I'm using the Google Maps API Web Services Directions API or the Distance Matrix API to find the travel distance and time between locations.
It seems that the API only supports calculation of the route between one origin and one destination. (In case of the Distance Matrix API, multiple combinations directly between origins and destinations are computed.)
My case is that I would like to know the route (really, just traveling time and distance) of a multiple-leg journey along a series (as in an ordered array) of locations.
So, considering four locations, A, B, C, D, I know how to compute the route between any pair, A-B, A-C, A-D, B-C and so on. I want to compute the route of A-B-C-D or A-C-D-B or A-D-B-Cand so on. I supply the route and the order of the legs, so I'm not asking to solve the Traveling Salesman Problem.
Is this possible with the Google Maps Directions API or Distance Matrix API?
Obviously, I can chain the locations myself, so ask for the routes of the individual legs in separate calls, and then add them together in the desired order: A-C + C-D + D-B becomes A-C-D-B. I'm hoping that it's possible with one call.
(I've looked for similar questions, but haven't found this exact one.)
Thanks to #geocodezip, the answer is to add 'waypoints' between the origin and the destination, as described in the documentation of the Directions API.
For example, the request https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA calculates the route between Boston and Concord via Charlestown and Lexington.
And the Traveling Salesman Problem is partly being solved as well, per
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.

Find city radius

I am working on an app where I need to check if some points are within a city or X km outside of it, so I need to find the city's radius then add the X variable to it. I've been looking on Google Maps and OpenStreetMap and I could not find such info.
Is there any API that provides the city's average width? Or distance between the center and the edge?
Based on the Latitude & Longitude of two given points, you can find the distance between those points.
So fetch the Location (Latitude & Longitude) for the centre of city and the point needed, using Google Maps API's and find out the distance implementing the logic.
Logic is described in below link :
http://www.movable-type.co.uk/scripts/latlong.html
Post calculation of distance, you verify if it is in the required range based on X.
I came across the same problem and here is the best method I found so far,
Using the service Geoapify
you can find the placeid for a city using the geocoding API
We can then create a location filter based on the place id as documented here
we can also use that filter for autocomplete to allow only for places inside the city

Google maps - find cities close to my route

I would like to get a list of all cities I might pass on my way between point A and point B
Input -
point A as origin
point B as destination
Output:
Route between point A and point B (that's obvious)
AND
list of cities / towns / places that are closer than X miles to my route.
I would like to present the user a list of possible waypoints to consider (points of interest, historic sites, parks, etc)
Any ideas how to do that?
I believe you'd need to do this in a two-stage process:
The first step would be to calculate a polyline representing the route between A and B (as you say, that's obvious). You can do this using the Bing Maps REST (http://msdn.microsoft.com/en-us/library/ff701717.aspx) or SOAP (http://msdn.microsoft.com/en-us/library/cc966826.aspx) routing services, for example.
When you request the route from either of these services, make sure you specify that you want the full route path to be included in the results (rpo paramter for REST or RoutePathType.Points parameter for SOAP). This will give you an array of all the points that are used to construct the routepath - otherwise you'll just get a summary of itinerary points (i.e. only those points on the route at which you need to do something - change roads etc.)
Once you've got the array of points, step two is to determine all those locations that lie within distance x of the path drawn between these points. While there are many webservices that allow you to query for places lying within x distance of an individual point (including the Bing Maps search service and the geonames findNearby service, for example), I'm not aware of any webservices that provide this functionality, so you'll have to provide it yourself.
One way of doing so would be to use SQL Server 2008 or SQL Server Azure, loaded up with the geonames allCountries data dump from http://download.geonames.org/export/dump/. You could then construct a LineString geometry from the points in your routepath, and query the list of places in the database with a query such as:
SELECT * FROM allCountries WHERE Location.STDistance(#RoutePath) <= 1000;
Johannes Kebeck posted an example using this approach based on the Bing SOAP Route service and SQL Azure, which you can find here: http://jkebeck.wordpress.com/2010/06/26/find-near-route-for-bing-maps-powered-by-sql-azure-spatial/

iphone application distance [duplicate]

I want to calculate the driving distance between two locations [the user's specified locations], before user starts the driving. I used many formulas and getDistanceFrom in CLLocation also, but I am not able to get the perfect the driving distance between those locations.
There is much difference in actual and calculated distance.
Please help me to calculate the driving distance between two locations.
How can I use Google Maps API to calculate the driving distance ?
I have already use the regexkitlite to show the road path on Google Maps between the two place. However I can't calculated the path distance.
If you have any code/link, please send it to me.
Thanks in advance....
See directions API of Google Maps
You have just to make a HTTP request:
http://maps.googleapis.com/maps/api/directions/output?parameters
Answer could be json or xml. You will find the distance in the answer
If you have two LatLongs you can calculate the direct distance between them; however, since you want driving directions (not as the crow flies) you'll need access to street-center-lines, so you can calculate the distance for each "leg" of the trip.
Google Maps may provide driving directions along with route distance, you'll need to check the documentation for that specific functionality.
edit
On a second look, it appears this may not be available on the API (as of 2006), but a work-around is posted here: http://groups.google.com/group/Google-Maps-API/msg/4dc2fad4f74e3314
Using Core Location API it will only give you the distance between two point in a straight line - most roads aren't, and hence the distance will be wrong.

Is there a way to determine if a particular address is along a route within x miles?

Is there a way to determine if a particular address is along a route within x miles? Is there support for this in the Google maps API?
I have a database of addresses and I am trying to figure out which locations lie along a given route as determined by the Google Maps API.
You can set the getPolyline option, on the GDirectionsOptions optional parameter, to the GDirections load request. This will get you the polyline data for the route.
Once you have this data you can iterate over each point in the polyline and determine the distance to each of your own datapoints (you can use the GLatLng distanceFrom method to calculate the distance).
Once you have the shortest distance to your route for each of your data points, you can and work out, based on some tolerance, if the point lies on the route.
Edit:
Although it is fine to call the GLatLng distanceFrom method repeatedly (it is just a utility method to get the distance between two points), I realized my answer simplifies the problem. To get an accurate distance from the route, you will need to determine the distance from the polyline between the closest two points (not just the distance from each point).
Bill Chadwick has "Distance of Point to Polyline or Polygon" code at the bottom of this page, which could prove useful.