HomePHP Page 2 - Parsing Google Maps API using PHP and JSON
Google Directions JSON API Explained - PHP
This Google Maps tutorial will walk you through the finer points of using the Google Directions REST API and JSON with PHP to load, parse and display your Google Maps. This article covers use of the Google API both with and without a Maps API key and includes PHP code examples for parsing the Directions or Web Services APIs from Google.
The array output should look like this in your PHP editor:
Note that the Google Maps API returns, with the Directions method applied, the following nodes- routes ·summary ·legs: 'n' number of legs for this trip. ·steps: 'n' number of steps to complete the travel of this particular leg of directions. ·duration: estimated time to achieve this direction detail step. ·html_summary: displayable directions of this particular step.
Now suppose you would like to know the “status” of the response. The answer for that is “OK” based on the array result. Alternatively, to extract this information using PHP:
echo $convertedtoarray['status'];
You need to remove the var_dump ($convertedtoarray); in the original script and then replace that with the code above.
What if you would like to access the result “Garden State Pkwy S”? This data is under [“summary”] array which is also under the array[0] and [routes], therefore:
echo $convertedtoarray['routes']['0']['summary'];
The above line will extract “Garden State Pkwy S” from the array and then display it in the browser.
Looking back, the original purpose of this tutorial is to find the driving distance between New York and New Jersey. A clue to finding the answer is in this array (found somewhere near the bottom of the array); the distance is around 82.6 miles: