When beginning to use Google Maps API on a project, you should realize there are two ways you can access the Google Maps data, depending on the depth of data you wish to retrieve and parse from Google: 1.) Accessing Google Maps data without the use of API key. The first type of application does not require your PHP applications to get an API key, however your project still needs to comply to the terms and conditions of Google Maps: http://www.google.com/intl/en_ALL/help/terms_local.html In your projects, you need to take note of this important clause in the terms and conditions: "Also, you may not use Google Maps in a manner which gives you or any other person access to mass downloads or bulk feeds of numerical latitude and longitude coordinates." Warning! This implies that you need to protect your applications from common abuse such as bot-automation, mass downloading of latitude/longitude coordinates, etc. You may also need to protect your PHP application with a Recaptcha: http://www.google.com/recaptcha, or limit access to the data users can download. You are responsible and Google will yank your API key in the event you are the source of abuse. The second type of application requires you to secure an API key for your project. However, in this beginner tutorial, we will focus on an authentication-keyless transaction with the Maps API. For now, all you need is some basic understanding about how Google maps data can be accessed using PHP and JSON. Understanding the Google Map Request URL Like all basic API applications (such as in Facebook, Yahoo site explorer, etc), they are similar in operations...as is accessing the Google Maps data in PHP. To have a basic grasp of how this works, look at the flow chart below:
It all starts with formulating the request URL to Google maps on the requesting/remote server using REST. Suppose, in this tutorial, that you are interested to find out the driving distance from Location_A to Location_B in miles or kilometers. You can do this using Google Maps in PHP. The Google Maps request URL for this application will be: http://maps.googleapis.com/maps/api/directions/json?origin=Location_A&alternatives=false&units=imperial&destination=Location_B&sensor=false There are a lot of request URLs for different types of Google map applications and you can read about them here: http://code.google.com/apis/maps/documentation/webservices/index.html However, I recommended that you read the above information only after you fully absorb the basics discussed in this tutorial. These request URLs belong to Google Maps API Web Services. As for the example request URL shown earlier, that belongs to the Google Maps Directions API: http://code.google.com/apis/maps/documentation/directions/ Let’s go back to the request URL to find the driving distance between Location_A and Location_B. For a better illustration, let's use a real world example. Suppose: Location_A = New York The revised request URL will be: http://maps.googleapis.com/maps/api/directions/json?origin=New%20York&alternatives=false&units=imperial&destination=New%20Jersey&sensor=false Remarks: More information can be found here: http://code.google.com/apis/maps/documentation/directions Since the directions API does not need an API key, you can even paste the above request URL in your browser. This will prompt you to download a file named “json”, which is the response from Google maps. When you open it with a text editor, you will see the following:
PHP Script to Send the Request/Retrieve data from Google Maps <?php //The request URL can be defined in the PHP variable $request='http://maps.googleapis.com/maps/api/directions/json?origin=New%20York&alternatives=false&units=imperial&destination=New%20Jersey&sensor=false'; //Now lets get the response of Google maps using PHP file_get_contents and then assign the response to $jsondata variable $jsondata = file_get_contents($request); //This is where things can get tricky; Google maps Json can be huge and sometimes confusing to extract the required information that you need because there is a lot of data in it. //For a simpler approach you can convert the Json format to a PHP array so that you will have a more comfortable and easy to understand data environment. //Use the PHP function to convert normal Json to array by jan at hooda dot de in php.net function json_code ($json) { //Finally you can convert the Google maps Json data to an array by using the function: $convertedtoarray = json_code($jsondata); //For testing purposes, you can dump the entire array data and then paste it to your favorite PHP editor for troubleshooting purposes: var_dump ($convertedtoarray); Keep in mind that your Apache install should be running the mod_json module installed and running or you'll not be able to parse the JSON response. JSON_Decode parses a well-formed JSON response into an associative array. You can download the test script here (as discussed above): http://www.php-developer.org/wp-content/uploads/scripts/testscriptgooglemapsphp.txt Run the code for this Maps API example in your local server (like XAMPP) and when the browser gives you the output, right click on it and view the source code. Press Control-A to select all and then copy all of it to your PHP editor. Save the array results as a temporary backup.
blog comments powered by Disqus |
|
|
|
|
|
|
|