Skip to content

Instantly share code, notes, and snippets.

@craigrodway
Created April 10, 2011 22:25
Show Gist options
  • Select an option

  • Save craigrodway/912796 to your computer and use it in GitHub Desktop.

Select an option

Save craigrodway/912796 to your computer and use it in GitHub Desktop.
Updated version of the gmaps plugin for CMS Made Simple. Static map for fallback. Code cleanup.
<?php
/*
CMS - CMS Made Simple
(c)2004-2007 by Ted Kulp ([email protected])
This project's homepage is: http://cmsmadesimple.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function smarty_cms_function_gmaps($params, &$smarty)
{
global $gCms;
$title = ($params['title']);
$info = ($params['info']);
$latlng = $params['latlng'];
list($lat, $lng) = explode(",", $latlng);
$zoom = isset($params['zoom']) ? $params['zoom'] : 12;
$width = isset($params['width']) ? $params['width'] : 250;
$height = isset($params['height']) ? $params['height'] : 250;
$maptype = isset($params['maptype']) ? $params['maptype'] : 'normal';
$dom_id = isset($params['id']) ? $params['id'] : "map" . uniqid();
// Map types. Static map uses different names than JS API.
switch($maptype){
case 'normal': $maptype2 = 'ROADMAP'; break;
case 'satellite': $maptype2 = 'SATELLITE'; break;
case 'hybrid': $maptype2 = 'HYBRID'; break;
case 'terrain': $maptype2 = 'TERRAIN'; break;
}
// Do static map first
$static_mapdata = array(
'center' => $latlng,
'zoom' => $zoom,
'size' => $width . 'x' . $height,
'maptype' => $maptype,
'markers' => 'color:red|' . $latlng,
'sensor' => 'false'
);
$static_mapurl = 'http://maps.google.com/maps/api/staticmap?';
$static_mapurl .= http_build_query($static_mapdata, '', '&amp;');
echo '<div id="' . $dom_id . '" style="width:' . $width . 'px; height:' . $height . 'px">';
echo '<img src="' . $static_mapurl . '" width="' . $width . '" height="' . $height . '" alt="Map: ' . $title . '">';
echo '</div>';
// Check for info window
$infowin = '';
if (!empty($title))
{
$infowin = 'var infowindow = new google.maps.InfoWindow({content: "<strong>' . $title . '</strong><br>' . $info . '"});';
$infowin .= 'google.maps.event.addListener(marker, "click", function(){ infowindow.open(map, marker); });';
}
// Output necessary javascript
echo '<script src="http://maps.google.com/maps/api/js?sensor=false"></script>'."\n";
echo <<< EOJS
<script>//<![CDATA[
(function(){
var latlng = "{$latlng}";
var map = null;
var latlng = new google.maps.LatLng({$lat}, {$lng});
var opts = {
zoom: {$zoom},
center: latlng,
mapTypeId: google.maps.MapTypeId.{$maptype2}
}
var map = new google.maps.Map(document.getElementById("{$dom_id}"), opts);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "{$title}"
});
{$infowin}
})();
//]]>
</script>
EOJS;
}
function smarty_cms_help_function_gmaps() {
?>
<h3>What does this do?</h3>
<p>This adds a simple Google Map on a page - first a simple image using Google
Static Maps API; and then if the browser supports Javascript, an interactive
map is added using the Google Maps API v3. No API key is required.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template or page like this:</p>
<code>{gmaps latlng="54.698477,-1.602577"}</code>
<br /><br />
<p>If you want to style your map, like adding a border, padding or whatever, you
must supply the <strong>id</strong> attribute in the tag. For example:</p>
<code>{gmaps latlng="54.698477,-1.602577" id="map-special"}</code>
<br />
<code>#map-special{ padding: 10px; background: #f00; margin: 10px; border: 1px solid #000;}</code>
<h3>What parameters does it take?</h3>
<p> </p>
<ul>
<li>
<em><strong>(required)</strong></em>
<tt>latlng</tt> - Latitude and longitude for centre of map and marker.
Use Google Maps to find this: right-click on location, centre map here,
grab the link, find the coordinates.
</li>
<li>
<em>(optional)</em>
<tt>id</tt> - Optional custom DOM ID of the map container.
</li>
<li>
<em>(optional)</em>
<tt>width</tt> - Width of the map in pixels. Default: 250.
</li>
<li>
<em>(optional)</em>
<tt>height</tt> - Height of the map in pixels. Default: 250.
</li>
<li>
<em>(optional)</em>
<tt>zoom</tt> - Map zoom level. Default: 12.
</li>
<li>
<em>(optional)</em>
<tt>maptype</tt> - One of normal, satellite, hybrid, or terrain. Default: normal.
</li>
<li>
<em>(optional)</em>
<tt>title</tt> - Title of the Point in Info window.
</li>
<li>
<em>(optional)</em>
<tt>info</tt> - Information/Description about Point in Info window.
</li>
</ul>
<?php
}
function smarty_cms_about_function_gmaps() {
?>
<p>Authors:
<a href="http://webman.me.uk/">Craig A Rodway</a> /
<a href="http://www.ich-mach-das.at">Goran Ilic</a>
</p>
<p>Version: 1.1</p>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment