PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Generating Static Images of Google Maps

Integrating Static Maps with a Geocoder

In the article Geocoding with PHP and the Google Maps API, we implemented a server-side geocoder, which allowed us to easily lookup an address or location and turn it into a longitude and latitude.

In this section we will combine the geocoder we create in that article with the static map generation performed in this article. Because both of these solutions don't use JavaScript, it makes it relatively simple to look up addresses and present maps to users, without requiring that they use JavaScript.

Listing 2 shows the code for the index.php file, which displays a form to the user so they can enter an address. This script also accepts and processes the form and queries the geocoder accordingly. For every placemark that is found, the formatted address is displayed as well as a static map of the location.

You will need the Geocoder.php, Placemark.php and Point.php files created in the other article. You can download them from the file listing for this article.

Note: Aside from the image generation, the code used in this example is very similar to that of the Geocoding with PHP and the Google Maps API article. Please read that article for details on how the geocoder works.
Listing 2 Accepting an address from the user and displaying maps for the entered location (static.php)
<?php
    $key = 'your key';
 
    if (isset($_GET['address']))
        $address = $_GET['address'];
    else
        $address = '';
 
    $placemarks = array();
 
    if (strlen($address) > 0) {
        require_once('Geocoder.php');
        $geocoder = new Geocoder($key);
 
        try {
            $placemarks = $geocoder->lookup($address);
        }
        catch (Exception $ex) { }
    }
?>
<h1>Static Google Maps</h1>
 
<form method="get" action="static.php">
    <div>
        Enter an address:
        <input type="text"
               name="address"
               value="<?php echo htmlSpecialChars($address) ?>" />
        <input type="submit" value="Submit" />
    </div>
</form>
 
<?php
    foreach ($placemarks as $placemark) {
        $point = $placemark->getPoint();
        $marker = $point->getLatitude() . ',' . $point->getLongitude();
?>
    <div>
        <h3><?php echo $placemark ?></h3>
        <p>
            Coordinates: <?php echo $marker ?>
        </p>
 
        <img src="http://maps.google.com/staticmap?key=<?php echo $key ?>&amp;size=500x300&amp;markers=<?php echo $marker ?>" />
    </div>
<?php } ?>

Figure 11 shows how running this script in your browser may look if you enter the address of the White House (that is, 1600 Pennsylvania Avenue, Washington DC).

Figure 11 Geocoding the White House and displaying a street map of its location
Figure 11: Geocoding the White House and displaying a street map of its location

In This Article


Additional Files