Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Parsing Google Maps API using PHP and JSON
  2. Google Directions JSON API Explained
By: Codex-M
Rating: starstarstarstarstar / 6
December 16, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

["distance"]=>
array(2) {
["value"]=>
int(132865)
["text"]=>
string(7) "82.6 mi"

You can confirm the accuracy using the actual http://maps.google.com/ and then inputting A for New York and B for New Jersey. Below is the result:

Here is the PHP line of code to extract this driving distance:

echo "Therefore the driving distance from New York to New Jersey is around: ".$convertedtoarray['routes']['0']['legs']['0']['distance']['text'];

The complete code for this project can be found here: http://www.php-developer.org/wp-content/uploads/scripts/googlemapssamplephp.txt . More examples about the Google Maps API in PHP will be covered in upcoming tutorials.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: