What I'm trying to achieve seems very common on the Google Maps front but doesn't show up in Drupal implementations. Basically, Google's example here: http://www.nycgo.com/?event=view.maps is a list of nodes in one column and their markers on a map next to it. When a node title is clicked, the appropriate marker's popup is activated.

The best solution I've figured out is to embed a view into the gmap-view-gmap.tpl.php using:

print views_embed_view($viewname, $display_id = 'default');

This doesn't allow for any interaction between the list and the markers but at least it displays two views on one page. Any thoughts on how to make the second view trigger markers in the GMap view?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hutch’s picture

A thought:
The gmap_location module already displays the map, it might be possible to add function theme_gmap_location_node_page() to your theme and tease out the markers from $map and with the help of some javascript build a list of links. Hypothetical but the bits you need are there I think. The jquery javascript would provide listeners to make the click do the right thing.

DanielWashbrook’s picture

Regarding the Javascript, the only ID I have found is the img's, 'mtgt_unnamed_0'. Is this the one that should respond to something like
$('#mtgt_unnamed_0').click();
or is there an ID for the marker as a whole?

Thanks!

hutch’s picture

I'm not sure, when look at the "generated source" using the web developer toolbar in firefox I can see id="mtgt_unnamed_0" so you are on the right lines I think, best thing is to try it and see if it picks up the links defined in Drupal.settings.

DanielWashbrook’s picture

I had to dig a bit in the DOM but I found it here: Drupal.settings.gmap.auto2map.markers[i].marker

so the code to trigger the marker looks like this:
GEvent.trigger(Drupal.settings.gmap.auto2map.markers[i].marker, "click");

Since I have a list of nodes that need to each trigger a different marker I used the bind() command to trigger the markers like this:

$('.views-field-title a').each(function(i){
	$(this).bind('click', function(){
		GEvent.trigger(Drupal.settings.gmap.auto2map.markers[i].marker, "click");
		return false;
	});
});

(The 'return false;' is just to disable the link) Hopefully this helps someone else down the road, perhaps a tutorial needs to be written about this one.

Thanks!

hutch’s picture

Thank you! You did the donkey work, I was just guessing ;-)
If you can write it up as a page on drupal.org that would be the most accessible.

DanielWashbrook’s picture

Good thinking. I've finished the rough draft, where would I post it? (I've never written up a page on drupal before). Should I join a specific group first?

Thanks!

** draft page **

GMap view - listing nodes with a GMap

By default the GMap view only allows for a map to be displayed with markers. To display a list with the Map and to allow for interaction between the two, follow these instructions:

First create your GMap page view. Try http://groups.drupal.org/node/19614

Ensure any specifics are added to the Defaults because the next step is to create a second "page" view but style it to display your node list. At some point you will want the "Fields" to be different and the "Path" will also differ. You will have two different pages now, one with a nodes list and one with a GMap that you'll want to combine into a single page.

Part of the difficulty is if you want to filter the results of both based on exposed filters. Add any exposed filters you like, but ensure they are in the Defaults so they apply to both the GMap and the node list and also ensure "Use Ajax" is set to "no".

To combine the two views into one, you must edit a file in the "gmap" module folder called "gmap-view-gmap.tpl.php" and add a call to embed your node list view with your GMap. The call will look something like this:

$viewname = "gmapview";
$output = views_embed_view($viewname, $display_id = 'default');
print $output;

A couple notes, the 'name' variable should be set to the name of your view and the 'display_id' should be either 'default' or the name of your node list page view.

The last step that is a bit complicated is having the link in the node list trigger the "click" event for it's marker. To accomplish this, create a copy of 'page.tpl.php' named 'page-[pathtotheview].tpl.php' in your theme folder. This will allow you to edit some Javascript at the bottom of the page at which point you can bind the javascript trigger like so:

<script>
            $(document).ready(function(){
                
                $('.views-field-title a').each(function(i){
                    $(this).bind('click', function(){
                        GEvent.trigger(Drupal.settings.gmap.auto2map.markers[i].marker, "click");
                        return false;
                    });
                });
                
                
            });
            
</script>

This script will execute once the page is loaded and bind all the links in each class "views-field-title" (this might be different but I used the "node:title" as my link) to a corresponding marker in the DOM. I think the 'auto2map' might be different in some cases but a look at the DOM should show you what the ID of your map is.

Once completed, a map and node list will be displayed and clicking on the title in the node list will trigger the associated marker.

scott_earnest’s picture

I am trying something similar but instead of updating the custom page tpl file I am creating an additional view display for "block", then set it to display in one of the sidebars on the path of the "page" display. The block display is using the same exposed filters as the default & page displays, but it is important to note that AJAX must be set to Yes for the exposed filters to work in the block, so override that setting.

I did have some issues where I was getting two sets of exposed filter forms, and neither one seemed to work. I've css'd out those forms (there's probablly a better way) but manually stuck in a 3rd one in the sidebar that works.

For me opening the info windows, this bit of code was key:

javascript:GEvent.trigger(Drupal.settings.gmap.auto1map.markers[0].marker, "click");

I was close to the correct code but not having the right element id for the map was tripping me up - thanks. I did try to put that code in the rewritten output of the field but js must be themed in (for me a row tpl file).

I used the "Global: View result counter" to help generate the offset of the link in the tpl file. Make sure that both block and page are set to the same default sort.

Also, having an "overflow: scroll" in the search results div gave a nice effect.

Thanks!

hutch’s picture

I'm not sure where this would best be posted, @YesCT, any ideas?

hillaryneaf’s picture

@Apero Media You could create a child page here in documentation (the link is at the bottom of the article): http://drupal.org/node/146590

johan2’s picture

What you also can do is making an attachment in your view (instead of a second page-view).
This attachment can be styled as a table, in the Attachment settings you can say yes to "Inherit" (from your page, wich is the map) and choose a position (after the page or before) and "Attach to: Page". Next in the theme information you can copy a .tpl for your tabel (views-view-table--Shop-locator.tpl.php), the name of your view is automatically correct. Next you put this tpl in your theme and add the script from #6 in it. In the gmap-view-gmap.tpl.php you add

$viewname = "Shop_locator";
$output = views_embed_view($viewname, $display_id = 'Attachment');
print $output;

Don't forget to refresh caches.

johan2’s picture

In order to style the table you also have to add a css class to your attachment (in the edit view), for example "shoplist".
Next you place some css in your local.css
.shoplist {
border-top: 1px solid #ccc;
background-color: #e8e8e8;
width: 900px;
height: 290px;
overflow: scroll;
}

.shoplist thead th {
background: black;
border-bottom: 1px solid #cfcfcf;
color: white;
visibility: visible;
overflow: visible;
}

In this way you have a scrollbar and a limited box in case you have a long list of locations.
For the moment the table head doesn't stick (don't use sticky header in your table view settings because it will place the labels at the top of the window). It would be nice if somebody knows how to arrange this...

nhoague’s picture

Hello,

Has anyone actually been able to get this to work? There has got to be something I am missing, I have followed this to a T!

Any advice above and beyond whats posted here that has helped anyone?

nhoague’s picture

I got it! Not sure what was different from yesterday, but I wasnt able to read into the 2nd page display. So I used johan2 idea and made an attachment. Now I am able to click the name of the node and the map marker corresponds to a node. Not the right one however because now I have random "Anonymous" showing up in my views table list. I can fix that however, another question. Any idea how I could make the

hoverable with the mouse to the correspond to the markers?

Thanks!

crutch’s picture

gmap 6.x-1.1 - I was successful with this method using a View and Attachment. The issue now encountering is that when clicking the "title" in the table, it opens a bubble, but +2 records ahead.

Record A - Click this
Record B
Record C - This bubble opens

crutch’s picture

solved my issue

I had two clickable/sortable columns on table display in my view attachment. They where linked as the first 2 records.

Now using 'unformatted' instead of 'table', and styled with css for a better fit beside map instead of under. Will provide table view with more detail at separate link for print friendly version.

ErikU’s picture

I would like to use a table Style for my view, but it seems to pop up the wrong location, the popup is 1 location ahead of the link. (my mistake... I edited the script in the template file and messed that up.)

Location #1: Click Link
Location #2: Shows Popup

BUT it still only works on the first two "locations".

netourish’s picture

Hi Everyone out there,

Thank you very much to all you people who worked into it , especially to "AperoMedia". This had really guided me in the right direction.

I was able to setup a simple html list with 4 items as titles on nodes in it, and a gmap in a block in the right side bar.

Now in the html list view, I have enable the pager with each page containing 2 items.

On page number 1 on clicking the items their respective locations are getting highlighted.

But in the second page on clicking the items, the first two location are getting highlighted once again. This should not happen, on clicking the items in the second page the location 3 and location 4 should be highlighted.

So, presently the scenario is something like this :-
present scenario :-

item 1 ==>> location 1
item 2 ==>> location 2
pager 1

item 3 ==>> location 1
item 4 ==>> location 2
pager 2

should be like this :-
item 3 ==>> location 3
item 4 ==>> location 4
pager 2

On reviewing the dom, I found that there are only 2 "links" available for every page in the pager. So, that is why I think, always the first two markers are getting highlighted according to the ace code of this entire beautiful post i;e "GEvent.trigger(Drupal.settings.gmap.auto2map.markers[i].marker, "click");".

I have also tried with enabling "ajax", did not made any difference.How can I achieve this. Any or all help/guidance is greatly honoured.

Thanking you all,
Pradeep

crutch’s picture

Pretty sure 'unformatted' has to be used to function correctly. Style with CSS for list of nodes, but not true ul or ol.

netourish’s picture

FileSize
973 bytes

Hi Crutch,

Thank you very much for your instantaneous response. I had changed the view style of node title listing from "html list" to "unformatted", but it did not made any difference.

Then, I decided to edit the views templates, by referring to this url :- "http://views-help.doc.logrus.com/help/views/using-theme".

And also made some changes to the js file to meet my requirements. This has been mentioned in the attachments. Please feel free to rectify me if I am incorrect regarding the concepts or drupal standards. Because, I am still very much new to this beautiful world.

Presently the site is enable with the "garland" theme and the view style is "html list".

So in the themes/garland directory a file has been created with name as "views-view-list--viewname.tpl.php" and the content of sites/all/modules/views/theme/views-view-list.tpl.php has been copied to it. Please refer to "http://views-help.doc.logrus.com/help/views/using-theme" for detailed reference.

Then, some changes have been performed to this file which is also mentioned in the attachment of this post.

By this way I was able to achieve the gmap location marking present in a blockjs from a html list present in a page content.

Thanking you all,
Pradeep

hillaryneaf’s picture

Is there anyway to do this without editing the gmap-view-gmap.tpl.php file in the gmap module folder?

This is not good practice because when the gmap module is updated, the code is gone and we need to fix it each time.

crutch’s picture

Correct, it is not good practice to edit anything in the module folders.

Copy the .tpl.php to your theme folder and config to use that instead of the one in the module folder. Will need to do some research to understand the process.

Contemplate module is also an alternative.

Module can then be updated without effecting theme/template.

hillaryneaf’s picture

Thanks, crutch it does work with the standard theming process. I followed #6 and assumed there was some reason the file in the module folder had to be updated rather than a copy in the theme folder.

hillaryneaf’s picture

One thing I noticed, not sure if it applies to anyone else. If one item in the list does not have a location set, the whole rest of the list links will sync up with the wrong items in the map since the marker array will no longer match up with each ".views-field-title a".

Is there a way to work around this? I think ideally if there is a location set it will use the click function, if there is no location set, it will just go directly to the node page.

Otherwise, I'll just update the view to ignore nodes without a location.

atodorov’s picture

Hi folks,
I just came across this thread and the example in comment #6 works great for me. I just need to show a map + a list of nodes.

However my view accepts arguments which are taxonomy term ids (multiple args separated by , or + allowed). The map correctly filters by the specified arguments but the embedded view does not. It shows all available items. Is there a way for the embedded view to accept the arguments from the map view or I need to manually program this?

atodorov’s picture

I figured this out myself:

<?php
// show a list view together with the map view
    $view_args = drupal_get_normal_path($_GET["q"]);
    $view_args = str_replace("taxonomy/term/", '', $view_args);
    $view_args = str_replace("/all", '', $view_args);

    $output = views_embed_view("taxonomy_map", $display_id = 'page_2', $view_args);
    print $output;
?>
ChrisValentine’s picture

FileSize
49.23 KB

I've got DanielWashbrook's combined view working, but I found I had to use the Default view for my list and use a second GMap Page view for the map (so you only need two views). I put the new template file into my theme (rather than module) folder and added the javascript to the end of that file - no other files needed editing. I also found that you can use Table as the Style for the list and apply sort orders and the link between map and list still operate correctly. You can add as many fields as you like for the list view but hide the ones you don't want displayed in the map pop-ups.

As suggested, any exposed filter must be defined for both the list and map views, but that results in two filters appearing on screen. Any idea of how to make it display only one?

And I can't get the javascript to trigger the map events. I found the embedded map id was 'gmap-auto1map-gmap0' instead of 'auto1map'. Obviously you have to make the list Node: Title field into a link, but it takes you to the individual node rather than triggering any map action.