PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.45 USD)

More information
Related Articles
Related Books
Beginning Google Maps Applications with PHP and Ajax: From Novice to Professional

Beginning Google Maps Applications with PHP and Ajax: From Novice to Professional

There is much to like about this book. The explanations are straightforward, the code is...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (11), JSON (2), MVC (1), MySQL (6), onbeforeunload (1), OOP (1), PHP (27), PhpDoc (1), PostgreSQL (6), Prototype (10), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

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)
<?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;
    }
?>

In This Article


Tagged in , , ,

Additional Files