I want to display a small Google map in a CCK node.

Currently location information is handled by the location module, and I can get all my nodes to display on a large map view, but if I want to display a single location on the associated node, I can't figure it out.

I've done some research and so far I have found out there's a module called Gmap views that can handle displaying a made within a CCK node. But it is redundant to have my nodes us two modules handling locations. I can't dump the location module in favor of Gmap view because I will lose the entire node collection in a large map.

Ideally, I'd like the location module to handle showing the node's location. I've brainstormed a way of doing this and implemented it, but it doesn't work out completely well.

Using this code inside a contemplate template:

<iframe width="48%" height="250" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=<?php print $node->locations[0]['lat'] ?>,<?php print $node->locations[0]['lon'] ?>&amp;ie=UTF8&amp;z=2&amp;ll=16.551962,42.84668&amp;output=embed&amp;s=AARTsJpuv4SdDc29NOa2Jg6C4Md2tLr6IQ"></iframe>

I get a map for only one of my nodes, I have figured out that the "s=AARTsJpuv4SdDc29NOa..." contains some information that prevents other locations from being displayed.

Anyways, that's what I have so far, any one have a way around this?

Comments

bestknight’s picture

Under blocks, enable the location map block for the content region and provided the node displayed has latitude and longitude set via the location module, a google map will appear below the node.

lupus78’s picture

Yes, that what i was looking for. Although i have some problems with it.
I set up the GMap macro code for the map, with specific zoom level, and controls turned on, but i only get a map zoomed to the maximum extent of my node locations, without any control!
I have more locations associated with my node (via location module)!

And lastly, as it's a block, I suspect it rather difficult to move it somewhere inside the node!? I mean displaying as part of the node, not underneath!

jendrek’s picture

The map does appear and the marker too. But I cant see any controls (zoom,...) and the marker is not in the center of the map.
Any way to solve this? Or is there a property to set.

confusion-1’s picture

I just fought with this issue. The problem appears to be that the controltype is hardcoded to "None".

In modules/gmap, edit the file gmap_location.module. Go to line 632 or search for "None". Change None to Large or Small, save and you're off to the races.

emersonlenon’s picture

By performing the above edit I was able to get the controls to appear.
Still the map does not center on the node's location properly. It looks like the map is centered on my default GMap lat and lon.
I also want to be able to do one thing further. I have two types of node on my site, both have 1 location. There are city nodes and intersection nodes and the intersection nodes reference the city they are in. So when you view a city node with the above technique it displays a map with a marker for the city and I want it to display markers for each intersection in that city. Is there a way to do that?

emersonlenon’s picture

So I managed to get my location map block to center on the node's lat and longitude.
Here is the edit I made to the gmap_location.module's function.
I changed it from:

function gmap_location_block_view($nid) {
  $block = array();
  $node = node_load($nid);
  if ($node->locations) {
    $markertypes = variable_get('gmap_node_markers', array());
    $markers = array();
    foreach ($node->locations as $loc) {
      // @@@ Todo: Client side geocoding
      if ($loc['latitude'] || $loc['longitude']) {
        $markers[] = array(
          'latitude' => $loc['latitude'],
          'longitude' => $loc['longitude'],
          'markername' => isset($markertypes[$row->type]) ? $markertypes[$row->type] : 'drupal',
        );
		
      }
    }
    if (!empty($markers)) {
      if (strlen(variable_get('gmap_location_block_macro_'. $node->type, '')) > 0) {
		$macro = variable_get('gmap_location_block_macro_'. $node->type, '');
      }
      else {
        $macro = variable_get('gmap_location_block_macro', '[gmap |id=block0 |' . $center_node . 'zoom=10 |width=100% |height=200px |control=Small |type=Map |behavior=+autozoom,+notype] ');
      }
      $map = gmap_parse_macro($macro);
      $map['markers'] = $markers;
      // @@@
      $map['behavior']['autozoom'] = true;
      $map['behavior']['notype'] = true;
      $map['controltype'] = 'Large';
      $block['subject'] = t('Location');
      $block['content'] = theme('gmap', array('#settings' => $map));
    }
  }
  return $block;
}

to:

function gmap_location_block_view($nid) {
  $block = array();
  $node = node_load($nid);
  if ($node->locations) {
    $markertypes = variable_get('gmap_node_markers', array());
    $markers = array();
    $i = 0
    foreach ($node->locations as $loc) {
      $i++
      // @@@ Todo: Client side geocoding
      if ($loc['latitude'] || $loc['longitude']) {
          if ($i == 1) {
	    $first_lat = $loc['latitude'];
		$first_lon =  $loc['longitude'];
		}
        $markers[] = array(
          'latitude' => $loc['latitude'],
          'longitude' => $loc['longitude'],
          'markername' => isset($markertypes[$row->type]) ? $markertypes[$row->type] : 'drupal',
        );
		
      }
    }
    if (!empty($markers)) {
      if (strlen(variable_get('gmap_location_block_macro_'. $node->type, '')) > 0) {
		$macro = variable_get('gmap_location_block_macro_'. $node->type, '');
      }
      else {
        $macro = variable_get('gmap_location_block_macro', '[gmap |id=block0 |zoom=10 |width=100% |height=200px |control=Small |type=Map |behavior=+autozoom,+notype] ');
      }
      $map = gmap_parse_macro($macro);
      $map['markers'] = $markers;
      // @@@
      $map['behavior']['autozoom'] = true;
      $map['behavior']['notype'] = true;
      $map['controltype'] = 'Large';
	  $map['latitude'] = $first_lat;
	  $map['longitude'] = $first_lon;
      $block['subject'] = t('Location');
      $block['content'] = theme('gmap', array('#settings' => $map));
    }
  }
  return $block;
}

I would still like to know how to display markers for every node that references a given node.

ellanylea’s picture

I'm using Gmap+Views to generate a map with data from CCK+Location nodes.

To also obtain one Gmap per node, without hacking modules (that's bad) or using blocks (less flexible placement), I created a node-mylocationtype.tpl.php file, and added this code above print $content; .

<?php
/*
 *  id - the id of the map every map on a page must have a unique id
 *  width - width of the map
 *  height - height of the map
 *  center - a string of the longitude and latitude of the centre of the map
 *  zoom - the zoom factor of the google map
 *  align - the alignment of the map 'right', 'left' or 'center'
 *  control - the control shown on the map 'Large', 'Small', or 'None'
 *  type - 'Map', 'Hybrid' or 'Satellite'
 *  points/markers - a string of points to mark on the map with + between each point
*/
    $points = array(
			'id' => 'map'.$node->nid,
			'width' => '530px',
			'height' => '320px',
			'latitude' => $location['latitude'],
			'longitude'=> $location['longitude'],
			'zoom' => 1,
			'maptype' => 'Hybrid',
			'markers' => array(array(
					'markername' => 'orange',
		      'latitude' => $location['latitude'],
		      'longitude' => $location['longitude']
		  ))
		);
    print theme('gmap', array('#settings' => $points));
    ?>

Since each mylocationtype node already has latitude and longitude, the map appears centered on that point.

Thanks to this thread.

Tina

---------------------------------------------------
AdAstra.ca - Optimize the Web. Optimize the World.

bwv’s picture

Hi Tina. This is very helpful, thank you. Could you please elaborate a little? For example, you say you "created a node-mylocationtype.tpl.php file, and added this code above." Then you provide the code. Would you be so kind as to outline specifically what you did? What does your new tpl.php file actually look like? Thanks very much, david

----------------------------------------------------------------------
http://www.bwv810.com/

I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.

rfay’s picture

Thanks, Tina, for this excellent example. It got me going fine.

Here is a more fleshed-out version.

First, what we're doing is theming a CCK node. You can get full information on how to theme a CCK node here: http://drupal.org/node/62462

In this example, I'm theming a CCK node type of "project" so the name of my theme file is node-project.tpl.php, and I put it in the directory of my theme.

This also adds a marker with info window using the title of the node. The info window did not work correctly for me at this time except with the current snapshot of the 5.x development version.

<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">

<?php print $picture ?>

<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

  <?php if ($submitted): ?>
    <span class="submitted"><?php print t('!date — !username', array('!username' => theme('username', $node), '!date' => format_date($node->created))); ?></span>
  <?php endif; ?>

  <div class="content">
  <?php
  /*
  *  id - the id of the map every map on a page must have a unique id
  *  width - width of the map
  *  height - height of the map
  *  center - a string of the longitude and latitude of the centre of the map
  *  zoom - the zoom factor of the google map
  *  align - the alignment of the map 'right', 'left' or 'center'
  *  control - the control shown on the map 'Large', 'Small', or 'None'
  *  type - 'Map', 'Hybrid' or 'Satellite'
  *  points/markers - a string of points to mark on the map with + between each point
  */
  $points = array(
     'id' => 'map'.$node->nid,
     'width' => '530px',
     'height' => '320px',
     'latitude' => $location['latitude'],
     'longitude'=> $location['longitude'],
     'zoom' => 9,
     'maptype' => 'Hybrid',
     'markers' => array(array(
       'markername' => 'blue',
       'text'=> $node->title,
       'markermode'=> 1,
       'opts'=> array('title'=> $node->title),
          'latitude' => $location['latitude'],
          'longitude' => $location['longitude'],
        )),
      );
    print theme('gmap', array('#settings' => $points));
?>

  <?php
    print $content;
  ?>
  </div>

  <div class="clear-block clear">
    <div class="meta">
    <?php if ($taxonomy): ?>
      <div class="terms"><?php print $terms ?></div>
    <?php endif;?>
    </div>

    <?php if ($links): ?>
      <div class="links"><?php print $links; ?></div>
    <?php endif; ?>
  </div>

</div>

ellanylea’s picture

Sounds about right to me. Here's an example of the end result: http://www.adastra.ca/travel/map/rwanda/kigali

And yes, with Drupal 5.7, the info window doesn't seem to work...

Tina

---------------------------------------------------
AdAstra.ca - Optimize the Web. Optimize the World.

demo9’s picture

This example, and the one above (by Tina) were both very helpful, as I ended up using a combination of both.

A couple of notes that may be of help to others. I didn't use a separate template file (as described above) but simply copied the required code into a CCK Template for the required Content type.
For this option to be available you must have the CCK module installed and have the 'Content Templates' option selected (Admin -> Modules -> CCK -> Content Templates)

As I needed the map display to be more flexible, I added a little bit more code. (I am based in Australia and the automatic lookup of coordinates doesn't work for me (yet?), so I have to enter the coordinates manually. This is fine, however, I use the maps with 'Events' and want some of my users to add their own events. If they do not enter the coordinates (or don't know how to get them) I end up with a page displaying a map of the 'Red Centre' of Australia (My default location).

I only want a map to be displayed if the LAT/LONG Coords have been entered....

So I surrounded rfay's code with

<?php if (isset($node->location['latitude']) && isset($node->location['longitude'])) { ?>
**Enter code here**
<?php } ?>

This works well for my intended purpose. I hope it is of use to others.

summit’s picture

Hi,

How about using cck map module instead?
Greetings, Martijn

gagarine’s picture

Tanks,
with http://drupal.org/project/contemplate i need juste this portion of code:

  /*
  *  id - the id of the map every map on a page must have a unique id
  *  width - width of the map
  *  height - height of the map
  *  center - a string of the longitude and latitude of the centre of the map
  *  zoom - the zoom factor of the google map
  *  align - the alignment of the map 'right', 'left' or 'center'
  *  control - the control shown on the map 'Large', 'Small', or 'None'
  *  type - 'Map', 'Hybrid' or 'Satellite'
  *  points/markers - a string of points to mark on the map with + between each point
  */
  $points = array(
     'id' => 'map'.$node->nid,
     'width' => '530px',
     'height' => '320px',
     'latitude' => $location['latitude'],
     'longitude'=> $location['longitude'],
     'zoom' => 9,
     'maptype' => 'Hybrid',
     'markers' => array(array(
       'markername' => 'blue',
       'text'=> $node->title,
       'markermode'=> 1,
       'opts'=> array('title'=> $node->title),
          'latitude' => $location['latitude'],
          'longitude' => $location['longitude'],
        )),
      );
    print theme('gmap', array('#settings' => $points));

and it's perfect :)

https://interface-network.com - Interface Network is an action and research technology governance agency.

bwv’s picture

I used the method employing contemplate and it worked very well. Thanks for the tip.
----------------------------------------------------------------------
http://www.bwv810.com/

I am a writer and researcher. In my spare time I build websites with Drupal.
Je peux communiquer en français. / Я могу общаться на русском языке.

gagarine’s picture

Hay... I don't know why but with the new version of gmap the marker are not displaying...

The bug here -> http://drupal.org/node/239120#comment-916941

https://interface-network.com - Interface Network is an action and research technology governance agency.

najibx’s picture

I got that working, but i want to use the cck location (not gmap addons). The latitude and longitude is save properly (in the node edit form). I am having tough time finding the right Current coordinates :

Replace this with xxx ?

 $location['latitude'],
     'longitude'=> $location['longitude'],

my location cck field is "field_location_map"

I see below, but doesnot mean much to me ..

$node->field_location_map[0]['lid']
    19 
$node->field_location_map[0]['view']
    <div class="location vcard"><div class="adr"> <em>My Place location</em><div class="street-address">Jln Kuah Langkawi</div> <span class="region">KD</span>, <span class="postal-code">07100</span><div class="country-name">Malaysia</div> <span class="geo"><abbr class="latitude" title="6.429857">6° 25' 47.4852" N</abbr>, <abbr class="longitude" title="99.685285">99° 41' 7.026" E</abbr></span> </div></div> 
najibx’s picture

The output from devel. Where's the field and value?


field_location_map

Array
(
    [0] => Array
        (
            [lid] => 19
        )

)

headphonesilence’s picture

This works perfectly for me!