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.
$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=" echo htmlSpecialChars($address) " /> <input type="submit" value="Submit" /> </div> </form> foreach ($placemarks as $placemark) { $point = $placemark->getPoint(); $marker = $point->getLatitude() . ',' . $point->getLongitude(); <div> <h3> echo $placemark </h3> <p> Coordinates: echo $marker </p> <img src="http://maps.google.com/staticmap?key= echo $key &size=500x300&markers= echo $marker " /> </div> }
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).

