Google Maps in Drupal 5

So, in the process of moving a static HTML site over to drupal 5, I had to deal with the DIRECTIONS page. Simple enough, yes? Yes. The page also had some embedded Google Maps. Simple enough to move over to drupal, yes?

[Note: there is a module for this, though it seems much more than we need here. Plus, it's marked beta.]

Yeah, not so much.

The set-up is simple enough. Pull in a javascript file from google. Add an empty <DIV> to your code, and then create a few simple javascript functions that insert your map of choice into that <DIV>.

First problem: grabbing the .js file from google. You might logically reach for drupal_add_js -- but, alas, it does not allow you add external files. (Though help could be on the way: #91250.) So you'll have to add it "manually." You could use drupal_set_header, but I chose to re-set the $scripts variable in the template.php file. Like such:

function _phptemplate_variables($hook, $vars = array()) {
  if ($hook=='page' && $_GET['q']=='directions') {
    $vars['scripts'] .= '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg" type="text/javascript"></script>';
    }
  return $vars;
}

Now we have to add those empty <DIV>'s. This is fairly easy, but just double-check your work, as WYSIWYGs and less permissive input filters can strip the ID's from your <DIV>.

Finally, we need to add the extra .js routines. The good news is that, here, you can use drupal_add_js. If you're using jQuery -- and you almost surely are -- it makes sense to rename your main function (e.g., gmap_initialize()) and make sure it loads. (There's no need to insert it into the <BODY> tag. So old-school. (Note, in drupal 6, the following code is even easier.)

$(document).ready(function(){
  gmap_initialize();
});

View the end result.