I was looking for a way to use the Google API for mapping in a block but that would work for every node in a particular content type. This code is based on the fact that a value is contained in a CCK field called field_address. If it's blank, the map will not display. If it's there, it will (there is no error checking on if it's valid other than a javascript popup). I am also not cleaning the address field because posts on my site are all moderated. I wrote this quick fix to tide me over until a good mapping module is ported to 6.x.

The address field can be either street,city,state or in the google maps lat/long format.

An example can be seen here: Day Trips Canada -- Simply click on any node from the front page to see how it works. Map/Real-time directions are at the bottom.

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if ($node = node_load(arg(1))) {
    if ($node->type == "trip") {
      if ($node->field_address) {
		if ($node->field_address[0]['value']) {
?>
<script src=" http://maps.google.com/?file=api&amp;v=2.x&amp;key=<put your key here>" type="text/javascript"></script>
<style type="text/css">
table.directions th {
background-color:#EEEEEE;
}
</style>
<script type="text/javascript">
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        setDirections("Toronto, Ontario", "<?php print_r($node->field_address[0]['value']); ?>", "en_US");
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
  
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
</script>
<body onload="initialize()" onunload="GUnload()">
<form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
<input type="hidden" id="locale" name="locale" value="en">
<b>From:</b> <input type="text" size="25" id="fromAddress" name="from" value="Toronto, Ontario"/>&nbsp;&nbsp;<b>To:</b>&nbsp;<input type="text" size="25" id="toAddress" readonly name="to" value="<?php print_r($node->field_address[0]['value']); ?>" />
&nbsp;&nbsp;<input name="submit" type="submit" value="Get Directions" />
</form>

<br/>
<table class="directions">
<tr><th>Formatted Directions</th><th>Map</th></tr>
<tr>
<td valign="top"><div id="directions" style="width: 280px"></div></td>
<td valign="top"><div id="map_canvas" style="width: 330px; height: 400px"></div></td>
</tr>
</table><br>
<small><b>How to use:</b> Type in your starting address by either City/Province or Street, City, Province and click the "Get Directions" button.</small>
<?php
      }
    }
  }
}
}
?>

Comments

sampeckham’s picture

Thanks for this tip.

To further elaborate, I combined some of this with playing around with Content Template module, to affect the RSS of one of my Content Types and created a GeoRSS to feed into the map. The difference this makes to the above, is having all your locations shown on one map.

Add Long/Lat fields to your content type (with CCK), then using the contemplate module, go to the RSS feed for that content type and add something similar to this.

<?php
  $latitude = $node->field_latitude[0]['value'];
  $longitude = $node->field_longitude[0]['value'];
  $geocode = $latitude.' '.$longitude;
  $xml_elements = array(
    array(
      'key' => 'georss:point',
      'value' => $geocode,
         ),
   );
?>

This will add the simple GeoRSS tags to your RSS.

Next create a View for the content type and create a 'feed' view. Set the path for that feed and add it to the 'initialize' function above. Or something like this will work.

function initialize() {
  if (GBrowserIsCompatible()) {
        geoXml = new GGeoXml("YOUR FEED HERE");
	map = new GMap2(document.getElementById("map_canvas")); 
    map.setCenter(new GLatLng(34.30714385628804, -2.109375), 1); 
	map.addControl(new GLargeMapControl());
    map.addControl(new GLargeMapControl());
    map.addOverlay(geoXml);
  }
}

I stuck my Google map in a block, to show all the points above a list of nodes.

daytripscanada’s picture

I am just posting a followup for this code as this post is very popular. I had originally used this code because there was no alternative Google maps solution for Drupal 6.x. Since then, gmap and location have been ported to 6.x.

Although this code still works and could be used for more custom work, I am now using the following modules to do the very same thing and then some:

http://drupal.org/project/gmap
http://drupal.org/project/location
http://drupal.org/project/views

I basically use a Location CCK to hold the address and/or GPS coordinates. Then I put the gmap in a block - example: http://www.day-trips.ca/content/distillery-district

I then use 'Views' to allow me to create custom node maps. For example, all nodes in a specific region/province - example: http://www.day-trips.ca/category/trips/ontario

I also use the default gmap nodemap option to create a map of ALL nodes - example: http://www.day-trips.ca/map/node

Hope this helps!

rromero3’s picture

I'm new in drupal and trying to learn and do somethings at the same time

can you help me guiding me on how to:
-put the gmap in a block
-use 'Views' to create custom node maps

i'm using 'location' to gather all my information

thanks

daytripscanada’s picture

You will need to use Views to make a new Block view. Once that's done, you can simply create a gmap view with whatever criteria and settings you want. Then place that block into your content.

Block Display the view as a block.
Basic settings
Name: Block
Title: None
Change settings for this styleStyle: GMap

{ Choose the data source for above GMap as Location module and enter your Gmap string:
[gmap zoom=5|center=53.56641415275043,-114.345703125|width=360px]
}

Change settings for this styleRow style: Fields
Use AJAX: No
Use pager: No
Items to display: Unlimited
More link: No
Distinct: No
Access: Unrestricted
Exposed form in block: No
Header: None
Footer: None
Empty text: None
Theme: Information

JoshOrndorff’s picture

I think I must be really misunderstanding something here. I'm making a site for a startup business, and I want to have a link in my primary links that says "Directions". That link would take that user to a page (or view or whatever else is necessary) that shows a map with a single marker on it. The marker of course would be where my business is located. I may also want to do fancier stuff like get the user driving directions, but for now I just want to make a map show up. It seems like that should be relatively simple or at least possible with a gmap module, but I just can't seem to figure it out. Any help would be greatly appreciated!

-Josh

Carlos Miranda Levy’s picture

For such a simple task, you can use the simple module Google Maps location, which does just that: google map for one location.
http://drupal.org/project/gmaplocation

abouch’s picture

@ anotheraviator
I am also a newbie, I got as far as setting up the views-block you described. but that's about it. Could you please explain how to use blocks on individual pages - mapping?
Also the theme you used is great, is it available in blue? I am building a community site.

Thanks,