Hi there, I have managed to get this working as I wanted, and I have to say it's fantastic. The only thing I'm really struggling with is dynamically generating a legend for the map.

http://www.carabusonline.co.uk/species_map

I know it would be quicker to just do this in gimp and stick in a png legend into the gmap_view footer text, but it's likely that the markers will be changed so would like a dynamic solution if possible. Any ideas where to even start?

CommentFileSizeAuthor
#8 legend.txt2.39 KBtom_o_t

Comments

tom_o_t’s picture

The solution I came up with for http://www.opengreenmap.org/en/greenmap was to use http://drupal.org/project/taxonomy_image

It's not a great solution as you have to duplicate the image used for the icon - once for gmap taxonomy markers, and once for taxonomy_image. But for me that worked well because I wanted different versions of the icons for the key and map anyway. You can create a block containing the taxonomy images with a bit of coding. I did a bunch of custom stuff using jquery to create a collapsible hierarchical key because there are so many different icons on the map, but for a simpler key this wouldn't be necessary.

I know that Bevan came up with a different solution for http://hub.witness.org/en/map - and I think he submitted a patch that included this. If you search around the issue queue and look for his posts you may be able to find it. It is probably closer to what you need.

Good luck - your site looks fantastic!

matthew_ellis24’s picture

Thanks, I'll give that a go. I've been thinking about it over night and I'm tempted to have a different icon for the different subgenera, which means 73 different icons in the 9 different subdivisions (ie 9 different shapes to represent the subdivisions and then different colours for the subgenera). I've not done any work with jquery but I'll have a look at that too as your key is more or less exactly what I want.

Many thanks
Matt

matthew_ellis24’s picture

I'm struggling with this key. I can get taxonomy images to display on species node pages but not on the gmap views. Presumably this is because the species node actually calls the taxonomy terms whereas the views page doesn't.

Is it necessary to call terms with images direct from the database? I presume not because if that was the case you wouldn't need taxonomy_image as the markers are saved to term ids anyway. And I hope not as that would require some nasty joins to get the hierarchy and images out :)

tom_o_t’s picture

I created a custom block that displays next to the map
The block's content uses the taxonomy_get_tree function http://api.drupal.org/api/function/taxonomy_get_tree/5 to get an array containing the terms for the appropriate vocabulary containing the key for the terms. Then I looped through all these terms to call the appropriate image for the term (there's a function in the taxonomy image module that does this). I then cached the resulting html code to save having to do this every time.

matthew_ellis24’s picture

ah ok many thanks. This is well beyond my skills at the minute so it might be a job for the future.

Thanks very much for your help

matthew_ellis24’s picture

Hi, for the idiots like me out there I've managed to get a very basic dynamic key together. As stated above you need to install taxonomy_image and assign images again, from what I can figure it's because gmap doesn't have any function for dealing with the images.

<?php
$vid=7; //vocab number
$parent=0;
$max_depth=3; //depth in tree to go to
$depth=-1;

$terms = taxonomy_get_tree($vid, $parent, $depth, $max_depth);  //gets all terms in tree=vid down to depth = max_depth
foreach($terms as $term){ //stores each term in an array

// the following variables store the array one term at a time
$tid=$term->tid;
$tname=$term->name;
$timage=taxonomy_image_display($tid, $tags = NULL, $profile = NULL, $overrides = array());

if ($timage){ //this is here to only display terms with images. Remove the if statement and one of the end curly brackets below to output all terms.
echo $timage." ".$tname."<br>"; //prints the list
}
}
?>

It would probably be better/more correct to print as an unordered list. This is quite unpretty but given everyone will want to theme differently this is probably a good place to leave it. Also I can't figure out how to make it prettier just yet :) but as tom_o_t says jQuery is probably the way to go

matthew_ellis24’s picture

I've tried adapting your script with a modicum of success. It's nearly there but I'm getting my logic all confused and things are popping out of my if loops in all the wrong places. My code is below. I know my problem is with the final bit of the elseif loop meaning that I get </ul> or </li> or both being printed before the actual lists are printed which of course really upsets it.

Any help would be greatly appreciated, especially as I can taste the end!

echo '<ul id="keys">';
$terms = taxonomy_get_tree($vid, $parent, $depth, $max_depth);  
foreach($terms as $term){ 

$tid=$term->tid;
$tname=$term->name;
$tdesc=$term->description;
$timage=taxonomy_image_display($tid, $tags = NULL, $profile = NULL, $overrides = array());  

if($tdesc=="subdivision"){ 

echo '<li id="key_genre_title_'.$tid.'" class="key_genre_title"><div class="key_title" onclick="javascript:toggleGenre(\'key_genre_content_'.$tid.'\', \'key_genre_title_'.$tid.'\');">'.$tname.'</div><div class="key_checkbox"><input type="checkbox" id="key_checkbox_genre_'.$tid.'" class="key_checkbox_genre" checked></input></div>';

}elseif($tdesc=="subgenus"){
echo '<ul id="key_genre_content_'.$tid.'" class="key_genre_content">';
echo '<li id="key_category_title_'.$tid.'" class="key_category_title key_expanded"><div class="key_title" onclick="javascript:toggleElement(\'key_category_content_'.$tid.'\', \'key_category_title_'.$tid.'\');">'.$timage.$tname.'</div><div class="key_checkbox"><input type="checkbox" id="key_checkbox_category_'.$tid.'" class="key_checkbox_category" checked></input></div>';

//echo '<div id="key_category_content_'.$tid.'" class="key_category_content"><span id="key_icon_'.$tid.'" class="key_icon">'.$timage.'</span></div>';

echo "</li>";

}

echo "</ul>";
echo "</li>";

}

echo "</ul>";

resulting page here
http://www.carabusonline.co.uk/content/taxonomy-tree-test

tom_o_t’s picture

StatusFileSize
new2.39 KB

Here's the code we used at Green Map. Basically I created a legend module with the display_key function in it. The function should be called with the parameter $icons, which is created using taxonomy_get_tree.

I'd love to turn this whole thing into an addon module for gmap, if there's enough interest, so I'd appreciate suggestions of improvements on how to do this. The code below is for the D5 version of the module, but I don't see anything obvious that would stop it working in D6. I've attached the code as a textfile too.

$icons = display_key(taxonomy_get_tree(1));


/**
 *	returns the key ui element
 *
 *	This function adds key.{css,js} to the output. /misc/menu-{collapsed,expanded}-green.png is required by the CSS.
 *	@param: $icons		a taxonomy array (generated by taxonomy_get_tree(1) )
 *	@return			(string)
 */
function display_key($icons)
{
	drupal_add_js(drupal_get_path('module', 'legend').'/key.js');
	drupal_add_css(drupal_get_path('module', 'legend').'/key.css');
	
	$key = '<form>';
	$key .= '<h3>' . t('Legend') . '</h3>';
	$key .= '<ul id="keys">';
	// iterate through all icons for the map
	foreach ($icons as $genre) {
		if (count($genre->parents) == 1 && $genre->parents[0] == 0) {
			// display genres
			$key .= '<li id="key_genre_title_'.$genre->tid.'" class="key_genre_title"><div class="key_title" onclick="javascript:toggleGenre(\'key_genre_content_'.$genre->tid.'\', \'key_genre_title_'.$genre->tid.'\');">' . t($genre->name) . '</div><div class="key_checkbox"><input type="checkbox" id="key_checkbox_genre_'.$genre->tid.'" class="key_checkbox_genre" checked></input></div>';
			$key .= '<ul id="key_genre_content_'.$genre->tid.'" class="key_genre_content">';
			foreach ($icons as $category) {
				if (in_array($genre->tid, $category->parents)) {
					// display child categories
					$key .= '<li id="key_category_title_'.$category->tid.'" class="key_category_title key_expanded"><div class="key_title" onclick="javascript:toggleElement(\'key_category_content_'.$category->tid.'\', \'key_category_title_'.$category->tid.'\');">' . t($category->name) . '</div><div class="key_checkbox"><input type="checkbox" id="key_checkbox_category_'.$category->tid.'" class="key_checkbox_category" checked></input></div>';
					$key .= '<div id="key_category_content_'.$category->tid.'" class="key_category_content">';
					foreach ($icons as $icon) {
						if (in_array($category->tid, $icon->parents)) {
							// display child icons
							$key .= '<span id="key_icon_'.$icon->tid.'" class="key_icon">';
							
							// TT: Resizing taxonomy images for key by overriding the size. This is basically a CSS resize. Quick.
							$key .= taxonomy_image_display($icon->tid, NULL, NULL, array('resize' => '1', 'width' => '21', 'height' => '18', 'imagecache_preset' => 'ORIGINAL'));
							
							$key .= '</span>';
						}
					}
					$key .= '</div>';
					$key .= "</li>";
				}
			}
			$key .= '</ul>';
			$key .= "</li>";
		}
	}
	$key .= '</ul>';
	$key .= '</form>';

	return $key;
} 
matthew_ellis24’s picture

ah, that would have been well beyond my drupalness. But I did learn that functions are your friends and that given enough time jQuery is good at making things pretty. Thanks I'll try and get this working.

matthew_ellis24’s picture

excellent, this works really well. Need to play around with the theming, but that should be very easy given the very neat classes you've laid out. I also need to figure out how the checkboxes work. I thought they were linked to a url argument, but there's no argument being parsed from what I can see so not sure how to work with them. The checkboxes themselves are turning up in a slightly odd place (inline with the titles but outside of the block region) which is no doubt a theming issue as well.

It's really spot on and I hope you manage to get this together as a full module as it's exactly what gmap needs.

Thanks again

tom_o_t’s picture

I should have taken the checkboxes out of the code I gave you - they interact with some crazy custom stuff that we created - they won't work with content loaded the standard gmap way. I'd recommend stripping out the checkboxes, unless you're a real javascript genius!

matthew_ellis24’s picture

I'm certainly not unfortunately! If you do develop this further they'd be quite handy. But as it is my hierarchical select menu is an OK way to filter them. Now to theme the key and make it look reasonably pretty. In fact the whole site could do with prettifying really. Ah, one day!

check boxes removed

matthew_ellis24’s picture

I've tried setting this up on a sister site
http://www.phanaeinionline.co.uk/species_map
the taxonomy here is a bit different and the custom markers are on the second, not third level. This appears to upset the code! I've tried reducing the max_depth in the hopes that this would force it to display the images

$icons = display_key(taxonomy_get_tree(2,0,-1,2));

Unsurprisingly that didn't work, but it did stop it trying to expand the second level terms. Obviously the problem is it's expecting parent->child->content whereas in my case the child is the content. Is this get-round-able?

matthew_ellis24’s picture

Got it. This is an edited version of the original code and displays icons for the bottom level of a two level hierarchy. Probably less use for this than the three level hierarchy supported by the original code, but here it is:

<?php


/**
 *	returns the key ui element
 *
 *	This function adds key.{css,js} to the output. /misc/menu-{collapsed,expanded}-green.png is required by the CSS.
 *	@param: $icons		a taxonomy array (generated by taxonomy_get_tree($vid) )
 *	@return			(string)
 */
function display_key($icons)
{
	drupal_add_js(drupal_get_path('module', 'legend').'/key.js');
	drupal_add_css(drupal_get_path('module', 'legend').'/key.css');
	
	$key = '<form>';
	//$key .= '<h3>' . t('Legend') . '</h3>';
	$key .= '<ul id="keys">';
	// iterate through all icons for the map
	foreach ($icons as $genre) {
		if (count($genre->parents) == 1 && $genre->parents[0] == 0) {
			// display genres
			$key .= '<li id="key_genre_title_'.$genre->tid.'" class="key_genre_title"><div class="key_title" onclick="javascript:toggleGenre(\'key_genre_content_'.$genre->tid.'\', \'key_genre_title_'.$genre->tid.'\');">' . t($genre->name) . '</div>';
			$key .= '<ul id="key_genre_content_'.$genre->tid.'" class="key_genre_content">';
			foreach ($icons as $category) {
				if (in_array($genre->tid, $category->parents)) {
					
					$key .= '<span id="key_icon_'.$category->tid.'" class="key_icon">';
					$key .= taxonomy_image_display($category->tid, NULL, NULL, array('resize' => '1', 'width' => '21', 'height' => '18', 'imagecache_preset' => 'ORIGINAL'));
					$key .= '</span>';
					
				}
			}
			$key .= '</ul>';
			$key .= "</li>";
		}
	}
	$key .= '</ul>';
	$key .= '</form>';

	return $key;
}

matthew_ellis24’s picture

remember to grant permission to "access taxonomy images" to whoever you want to be able to see them!

bartezz’s picture

subscribe

mandclu’s picture

Subscribe

podarok’s picture

Status: Active » Closed (won't fix)

release unsupported
feel free to open issue against latest 7.x dev

jkingsnorth’s picture

Version: 5.x-1.0-rc2 » 7.x-2.x-dev
Component: Miscellaneous » Code
Category: support » feature
Status: Closed (won't fix) » Active

Adding this back as a feature request against the latest dev - some way to create an automatic legend of markers and associated taxonomy terms would be great.