Improving the Javascript
Sun, 06/01/2008 - 9:08pm — admin
In the earlier postings, I had a relatively crude method of adding the javascript by calling drupal_add_js in my template.php file. The main problem with this method was that it included the map on every page regardless of when it was needed.
To improve this, I used the phptemplate_variables function, and within the node case, I do a text to see whether we are on an event page, and if so, I add the javascript files.
<?php
function _phptemplate_variables($hook, $vars = array()) {
case 'node':
if ($vars['type'] == 'event' && !$vars['teaser']) {
// Add the Mapstraction and event JS
$path = drupal_get_path('theme', 'zen');
drupal_add_js($path.'/js/mapstraction.js',$type = 'theme');
drupal_add_js($path.'/js/events.js',$type = 'theme');
}
}
?>
I'm not sure whether this is any better, but it does work. The mapstraction.js and events.js files are only used on event pages. [ I haven't done anything about the google maps js file. That's still hard coded in the page.tpl.php file]. I had a closer look at the events.js file, which creates the map. Up until now, it used the same centre point for each map. By teaking the code, I can centre the map on the location in question, and therefore zoom in a little more. // Geocode each hcard and add a marker $('.node').each(function() { var hcalendar = $(this); var latitude = hcalendar.find('.geo .latitude').text(); var longitude = hcalendar.find('.geo .longitude').text(); // Show map centred on Event Location mapstraction.setCenterAndZoom( new LatLonPoint(latitude, longitude), 10 // Zoom level appropriate for Ireland ); The maps look all the better for it.
