Php – Using Google Maps API to find routes & distance between two locations and present it in a table

google mapsgoogle-maps-api-3MySQLPHP

I want to use Google maps to find out available routes between two cities. Some information about these routes like distance and travel time etc.

I do not want this data to be presented in a Map. I need this to be presented to end user in textual format.

User will give me two cities and I will present him with various routes and information in a table.

Can this be done using PHP, MySQL and Google Maps? Any pointers would be very helpful.

Best Answer

If you want multiple routes between two points, use the directions service (http://code.google.com/apis/maps/documentation/directions) and specify alternatives=true in the request. This will return alternate routes, if they're available. Then, parse the response for the desired information:

  • there will be one legs array for each route
  • within that legs array, there is a distance and a duration field, each containing a raw value and a textual value.

If you don't need multiple choices for the route, the Google Maps Distance Matrix service (http://code.google.com/apis/maps/documentation/distancematrix/) provides travel distance and time between multiple origins and destinations, without all of the directions information.

A sample table is in the blog post: http://googlegeodevelopers.blogspot.com/2011/05/what-is-distance-matrix.html