Drupal 5 Custom GMap Node

I really enjoy Google Maps. And though they keep a little for themselves, the Google Maps API offers a really easy way to embed these maps into your own personal site. Fortunately for us Drupal users, the swell folks at the Chicago Technology Cooperative and the other wonderful open-source developers have put together an amazing module called GMap that integrates well with Location, Views, and even CCK (sort of).

I have recently set up this site to use the GMAP module. And since this module is still not had a stable release yet, I figured I would explain what I have done with this site to get some custom maps on each node. I will go through setting up CCK fields, custom theming, and some extras.

In Action

First, have a gander at what this will look like once we are done.

Goals

Given that I already have a bunch of nodes to create a Map View with, I want to also display a map on each of the nodes for that location or place. I also want the node maps to be customize for each node.

The main reason I would like this functionality is because I think in the context of a node that is a location, it should have a map with the node itself. Also, I think its a really nice feature to have control how that map looks. There are certain locations that I would like to be zoomed in real close, or that I want a map view, or a satellite view. This will give us that control.

Assumptions

The following is a list of assumptions in this process. If the following are not true, then this tutorial may not work for you.

GMap Bugs

There a couple things to note about this version of the GMap Module:

  • You will need to hack the GMap Module so that it uses the most recent stable version of the Google Maps API. See http://drupal.org/node/231473.
  • There are some inconsistencies between the API.txt file and the actual way to use the GMap API. See http://drupal.org/node/236352 for more information. For our purposes, the following needs to be noted for the associateive array that will be used to create each map:
    • Use controltype instead of control.
    • Use maptype instead of type.

Map Content Type

The first thing we need to do is set up a custom Content Type. This is where CCK and Location are going to come in.

Locative Information

First ensure, that Locative Information is added. When creating the Content Type, it will be at the top of the form. It's kind of hard to notice. Make sure that Maximum number of locations allowed for this type. is at least 1 and Default number of location forms is also at least 1. Plaese note that this tutorial is assuming one location per node. This could easily be changed so that it handled more that one location. Enabling this Location information, will ensure that Latitude and Longitude are collected. Again this could be changed to CCK fields instead.

Screen: Locative Information on Content Type Creation: Screenshot to point out where the Locative Information section is when creating a new Content Type.
Screen: Locative Information on Content Type Creation: Screenshot to point out where the Locative Information section is when creating a new Content Type.

CCK Map Parameters

Now you will want to create a set of fields to represent your map parameters. You can add or remove the ones you want. You will want to look at the GMap Macro Creator page that comes with GMap. Here is a brief description of my fields:

CCK Map Macro Parameters
Name Machine Name Type Widget Type
Map Width field_cck_txt_map_width Text Text Field
Map Height field_cck_txt_map_height Text Text Field
Marker field_cck_select_marker Text Select List
Magnification field_cck_select_magnification Text Select List
Control Type field_cck_select_map_control Text Select List
Map Type field_cck_select_map_type Text Select List

Screen: Macro Parameters as GMap Node Fields: Screenshot of the CCK fields that are used to create a custom GMap per node.
Screen: Macro Parameters as GMap Node Fields: Screenshot of the CCK fields that are used to create a custom GMap per node.

The Select Lists should be filled appropriately from the GMap Macro Creator page. Hint: changes some values and note the macro at the bottom. Please note that these values are Case Sensitive.

Theming

Now that we have our content type set up, now we just have to determine how it is going to render all this information. Feel free to make a couple nodes with this new content type, as it will help you see what is happening.

Display Function

We will create a function in our template.php file, in our theme directory, so that calling the map in the theme will be simple.

  1. #function to make Gmap macro data into a map
  2. function your_theme_node_map_maker($id, $mark, $lat, $long, $zoom, $width, $height, $control, $map_type) {
  3. #intialize output
  4. $output = '';
  5.  
  6. #put together array
  7. $arr_map = array(
  8. 'id' => $id
  9. ,'zoom' => $zoom
  10. ,'width' => $width
  11. ,'height' => $height
  12. ,'latitude' => $lat
  13. ,'longitude'=> $long
  14. ,'maptype' => $map_type
  15. ,'controltype' => $control
  16. ,'markers' => array(
  17. 'markername' => $mark
  18. ,'latitude' => $lat
  19. ,'longitude' => $long
  20. )
  21. )
  22. );
  23.  
  24. #display map through GMap theme function
  25. $output = theme('gmap', array('#settings' => $arr_map));
  26.  
  27. #return output
  28. return $output;
  29. } #end function

This should be pretty straightforward. It would not be too difficult to actually put this in the theme itself, but this way we could do some processing if necessary.

Calling the Function in the Theme

There is a couple ways to do this depending on how you like to make or edit themes. You can use the Content Template Module. But I like to stick with the traditional file method.

Here is my sample node-ct-map.tpl.php file:

  1. <?php if ($page == 0) { ?><h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2><?php }; ?>
  2.  
  3. <div class="entry<?php if ($sticky) { print " sticky"; } ?>">
  4. <?php if ($picture) { print $picture; }?>
  5.  
  6. <div class="ct-map-content"><?php print $node->content['body']['#value'] ?></div>
  7.  
  8. <fieldset class="fieldgroup group-visits">
  9. <legend>Visits</legend>
  10. <?php print alanpalazzolo_custom_visits($node->field_cck_date_location_start, $node->field_cck_txt_visited_descripti) ?>
  11. </fieldset>
  12.  
  13. <?php if ($node->content['field_cck_img_map_images']['#value']) { ?>
  14. <fieldset class="fieldgroup group-map-pictures">
  15. <legend>Pictures</legend>
  16. <?php print $node->content['field_cck_img_map_images']['#value'] ?>
  17. </fieldset>
  18. <?php } ?>
  19.  
  20. <?php
  21. # Call Map Function
  22. # I have separated the parameters so that it easier to see
  23. print alanpalazzolo_custom_node_map_maker(
  24. $node->nid, $node->field_cck_select_marker[0]['view']
  25. ,$node->locations[0]['latitude']
  26. ,$node->locations[0]['longitude']
  27. ,$node->field_cck_select_magnification[0]['view']
  28. ,$node->field_cck_txt_map_width[0]['view']
  29. ,$node->field_cck_txt_map_height[0]['view']
  30. ,$node->field_cck_select_map_control[0]['view']
  31. ,$node->field_cck_select_map_type[0]['view']
  32. ) ?>
  33.  
  34. <?php print alanpalazzolo_custom_location($node->location) ?>
  35.  
  36. </div>
  37.  
  38. <div class="meta">
  39. <p class="byline"><?php print $submitted?></p>
  40. <?php if ($terms) { ?><p><span class="taxonomy"><?php print $terms?></span></p><?php }; ?>
  41. <?php if ($links) { ?><p class="links"><?php print $links?></p><?php }; ?>
  42. </div>

Check Our Work

Now, when going to the nodes that we have created, there should be a map with the parameteres we have set on them. For instance:

Views

This should be a fairly easy process. Create a new view.

  • Page Section

    • click Provide Page View
    • choose a URL
    • this is where the magic happens; under View Type choose GMap View
  • Fields Section
    • Location: Latitude
    • Location: Longitude (This is where the CCK fields could be substituted.)
    • I would also suggest choosing the Node: Title field so that there is link in each info bubble
  • Filter Section
    • Node: Published is Yes
    • Node: Type is One Of Map Type (This is the type that we defined above)

Questions

Please feel free to add a comment or contact me about this post. This is just a brief look into what can be done with these powerful technologies.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.