Add Google Maps with UDT

Often there is a need to display a Google Map somwhere on the Website, but do you realy want to go through trouble of installing CGGoogleMaps module and dealing with fopen issue if it's disabled and you don't have access to change this on the Server just because of one single Google Map and a single Point?

I did often bump in this issue or dealing with errors just because of disabled fopen on the Server so i decided to put together a small UDT for cases like this.

First of all i want to say that this is not some perfect solution and could for sure be done better, but i am not a programmer and this small UDT serves me well enough.

The code i use looks like this:

global $gCms;
$name    = $params['name'];
$info    = $params['info'];
$address = $params['address'];
$mapkey  = $params['mapkey'];
$zoom    = $params['zoom'];
$width   = $params['width'];
$height  = $params['height'];
$name .= '' . $value;
$info .= '' . $info;
$address .= '' . $value;
$mapkey .= '' . $value;
$zoom .= '' . $value;
$width  = 'width:' . $width . 'px;';
$height = 'height:' . $height . 'px;';
$string = <<<EOT
<script src="http://maps.google.com/maps?file=api&v=3&key=$mapkey" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8"> //<![CDATA[
if (GBrowserIsCompatible()) 
{document.write('<div id="map" style="$width $height"><\/div>');
} 
else 
{document.write('<b>Javascript must be enabled in order to use Google Maps.<\/b>');}
//]]></script>    
<noscript><b>Javascript must be enabled in order to use Google Maps.</b>  </noscript>  
<script type="text/javascript">     
//<![CDATA[
var address = "$address";        
var map = null;    
var geocoder = null;         
function addToMap(response) 
{           
setaddress = response.Placemark[0];           
point = new GLatLng(setaddress.Point.coordinates[1],setaddress.Point.coordinates[0]);           
map.setCenter(point, $zoom);           
marker = new GMarker(point);           
map.addOverlay(marker);           
marker.openInfoWindowHtml('<div style="width:240px"><strong>$name<\/strong><br \/>$info <\/div>');        
}             
if (GBrowserIsCompatible()) 
{            
map = new GMap2(document.getElementById("map"));            
map.setUIToDefault();            
geocoder = new GClientGeocoder();            
geocoder.getLocations(address, addToMap);             
}     
//]]>    
</script>
EOT;
echo $string;

And thats it, a simple Google Maps implementation. As i said above i am not a programmer but this could also be extended, since if parameters are left blank you could get in trouble.
A working example can be found here

Update: I spent some time and rewrote this UDT as a plugin, so if you need a simple Google Map with no Module overload you can go and get it in Forge http://dev.cmsmadesimple.org/projects/gmaps

Comments