Geocoding with PHP and the Google Maps API
Using the Geocoder
Now that the Geocoder, Placemark and Point class are complete, we can make use of our geocoding implementation. To do so, we will now create a simple web-based interface to the geocoder. This script will display a form with a single text input, which allows the user to enter their desired location to find coordinates for. When they submit the form, the request will be sent to Google and the results will be displayed to the user.
The code for this script can be written to any file you like, but I have called it index.php for the sake of this article. The full script is available from the article file downloads section.
This code works by displaying a form that submits back to itself. At the start of the class we check if the address value has been submitted, and if so a geocoder request is performed.
Listing 19 shows the beginning of the index.php file, which first checks the $_GET variable for a submitted address. If there is a value, the Geocoder class is instantiated and a request is performed. I have also included some basic exception handling. That is, if an error occurs (such as invalid key being specified), the code is displayed and the script exits.
if (isset($_GET['address'])) $address = $_GET['address']; else $address = ''; $lookupPerformed = false; if (strlen($address) > 0) { require_once('Geocoder.php'); $geocoder = new Geocoder('your key here'); try { $placemarks = $geocoder->lookup($address); } catch (Exception $ex) { echo $ex->getMessage(); exit; } $lookupPerformed = true; }
Next we include the HTML output in the index.php script, as shown in Listing 20. At the top of the page we show the form, which consists of a text input and a submit button. If a request has been submitted we re-display that value in the text input.
<html> <head> <title>Google Geocoder</title> </head> <body> <div> <form method="get" action="index.php"> <div> <input type="text" name="address" value=" echo htmlSpecialChars($address) " /> <input type="submit" value="Lookup" /> </div> </form>
$address using htmlSpecialChars() to prevent malicious code from being executed (or to prevent characters being displayed that will break the HTML output, such as ").Next we check if a geocoder request has been performed (using the $lookupPerformed variable created in Listing 19), and if so, we output the results from the request. Listing 21 shows the code used to do this, which outputs each returned placemark with its formatted and its coordinates.
if ($lookupPerformed) { <hr /> <h2>Geocoder Results</h2> if (count($placemarks) > 0) { <dl> foreach ($placemarks as $placemark) { <dt> echo htmlSpecialChars($placemark) </dt> <dd> Latitude echo $placemark->getPoint()->getLatitude() , Longitude echo $placemark->getPoint()->getLongitude() </dd> } </dl> } else { <p> No matches found for <strong> echo htmlSpecialChars($address) </strong> </p> } } </div> </body> </html>
This now concludes the code required to use the geocoder. In Figure 1, I have shown the output when querying main st, usa. This is an ambiguous location that results in many matches from the geocoder.


