Generating Static Images of Google Maps
Generating Image Requests with Smarty
As an added bonus, I've included here a Smarty plug-in you can use to easily generate images. Without giving too much of a description, using this plug-in you will be able to use the following in your PHP code:
Listing 3 Sample usage of the getgooglemapimageurl plug-in (listing-3.tpl)
<img src="{getgooglemapimageurl w=500 h=300 latitude=$lat longitude=$lng key=$key}" />
Listing 4 shows the code for the getgooglemapimageurl plug-in, which belongs in a file called function.getgooglemapimageurl.php in your Smarty plug-ins directory.
Note: This code comes as-is with little description, and is intended for users with some knowledge of implementing Smarty plug-ins. For more information on Smarty, you can refer to the Smarty section of PhpRiot
Listing 4 The getgooglemapimageurl plug-in for Smarty (function.getgooglemapimageurl.php)
function smarty_function_getgooglemapimageurl($params, $smarty) { $baseUrl = 'http://maps.google.com/staticmap'; $defaults = array( 'key' => '', 'longitude' => '', 'latitude' => '', 'w' => 0, 'h' => 0, 'type' => '', ); $types = array('roadmap', 'mobile'); foreach ($defaults as $k => $v) { if (!array_key_exists($k, $params)) $params[$k] = $v; } if (strlen($params['key']) == 0) $smarty->trigger_error('getgooglemapimageurl: you must specify your Google Maps API key', E_USER_NOTICE); $params['w'] = min(512, (int) $params['w']); $params['h'] = min(512, (int) $params['h']); if ($params['w'] <= 0) $smarty->trigger_error('getgooglemapimageurl: you must specify the map width in pixels', E_USER_NOTICE); if ($params['h'] <= 0) $smarty->trigger_error('getgooglemapimageurl: you must specify the map height in pixels', E_USER_NOTICE); // setup the required parameters $url = sprintf('%s?key=%s&size=%dx%d', $baseUrl, $params['key'], $params['w'], $params['h']); // specify the map type if (in_array($params['type'], $types)) $url .= sprintf('&maptype=%s', $params['type']); $url .= sprintf('&markers=%lf,%lf', $params['latitude'], $params['longitude']); return $url; }




