Contextual Filters in Openlayers Example

Introduction

One of the recent comments to come through was about implementing dynamic filtering on the Openlayers. Filtering is setup the same way as you normally would for views that only caveat is the the filtering will happen on the Openlayers Display Overlay but the rendered out will be to happen on a page or block display. The example I will be using to demonstrate Contextual Filtering on Openlayers will be a map, which displays different types of restaurants in a fictitious place called Springfield. I'll use a couple of categories of restaurant, Indian & Thai and provide a side-bar which allows you to select either type.

Pre-requirsite

Read and view some of the views contextual filters (views arguments) tutorials available on the net and of course you will need to have done this one

Drupal Topics Covered

  • Openlayers
  • Geofield
  • Views contextual Filers
  • Taxonomy Terms

Method

  • I'll create a new map via Openlayers, then a Openlayers view and setup a map at the location of springfield_restaurants. All of the steps are discussed here
  • I'll create a new content type and add a geofield for the restaurant and a taxonomy term to specify the type of restaurant, also I'll add a new taxonomy vocabulary called Restaurant Type, which I'll assign the taxonomy terms too.
  • Now I'm going to create a couple of Springfield restaurant content types to generate data for the following setup.
  • Once I have the initial map setup, I can then start planing on filtering the restaurants to taxonomy:term name type. Now I want to filter by taxonomy name instead of term id, this has a advantage navigating the site with a name instead of attempting to find the term id, though it is better practice to use term id.
  • To be able to use extra taxonomy contextual filters we will need to add a Relationship on the Openlayers Display Overlay Display and select the 'taxonomy terms on node'. The select your vocabulary in my case it will be restaurant type.
  • Now add the 'Taxonomy term: Name'. this has been provided by the Relationship we just setup. Below is how I set up my settings for the 'Taxonomy term: name' contextual filter

Taxonomy: name (top)

Taxonomy term: name (top)



  • Now go back to the path defined in the page view and enter your term name to check the contextual filters are operating correctly. In my case I'll enter the 'thai' argument after /springfield_restaurant path. Also you will notice that without any term arguments we get all the dots showing, this is because we selected 'Display all results for the specified field' in the contextual filter
  • Now lets create a menu bar that provides the restaurants type in the sidebar. First we will create a new view based on taxonomy terms, here are my initial views settings

Taxonomy term view



  • Fill in a block name
  • Add the 'Taxonomy term: vocabulary' to the filter criteria and select your vocabulary type.
  • That should be it for this view, here are my settings.


Taxonomy term block display



  • Now go into the Block settings and add the new block view to the sidebar.
  • Save that then configure the block and select the 'Show block on specific pages' and select where you want the menu to be displayed. Below are my settings to display the main map and then the following contextual field arguments

Menu block specific page



  • Now when we go to the restaurant page we see the menu, but unfortunately the links go the to content pages themselves, we want them to select the restaurant types on the map instead. So we will go back into the block display and rewrite the results of the taxonomy term: name link. I've used this technique several times on previous tutorials, so I won't go into detail for this.
  • Once that has been saved go back to the map page and test the menu links. That should now be behaving as we intended, below is an example of what you should have completed

Final map



Conclusion

The new skills you would have picked up if you followed the previous Openlayers tutorials are using views contextual filters to control what terms are display on a map. There are probably a dozen ways in Drupal you could achieve this, but this is the method that instinctively came to my mind.

For any questions or comments about the tutorial post a comment at the end of this page

Bye for now and remember don't forget to flush your cache.

Comments

Submitted by Eoin (not verified) on

Hi Nigel,

Thanks a million for this tutorial. Exactly what I was looking for.

Just one bug that I've noticed for my case anyway, is that if you create a content type (restaurant) and don't assign a geofield. Then that content type gets assigned on the map to the next geofield that is assigned. Will see if I can see what's going on but can work around it for the moment anyway.

Thanks again!!

Submitted by nigel on

Hi Eoin, I had a bit of a play around and managed to reproduce the behaviour you are seeing. I think we both assumed that the Geofields would have associative behaviour with the content types they are assigned to but that is not the case. I don't know if this is a bug or just the way that Geofields have been implemented. It may be worth a query on the Geofields issues list.

You mention you have a workaround, here's mine, I went into the resturant content type taxaxonomy term settings and set a default value of undefined so that all new restaurant types will have this value set by default when the Geofield dots have not yet been set.  You will also need to add a taxonomy term filter to both the map and menu views that is set to '!= undefined' to ensure the undefined types are not displayed. This may seem a little kludgy but views is designed to sort out this kind of thing.

Was your workaround similar? 

Submitted by Rui (not verified) on

Hi Nigel,
thank you for this nice and informativ tutorials with Drupal and Openlayers. I learned a lot.
This tutorial worked fine for me so far, but i´m now lost in trying to show the different restaurants with different markers. It could although be another taxonomy, perhaps restaurants with stars. One star if not so good and three star if very nice and tasty food. Or a flag for indian and a flag for thai restaurants. I´ve tried Openlayers Taxonomy but ist seems be broken. I´ve tried different layers with different png´s. But it isnt´t shown up. Have you an idea, how to solve this?

Submitted by nigel on

You will also need to create a Geofield for each of the stars styles and enable all the star styles in your map. So each of your map's content type will have 3 geofields representing a  number of stars and you will just need to select one of them.

Submitted by Laurent (not verified) on

Hi Nigel,

First of thanks for those tutorials, I really appreciate them, and they helped a lot.
But just as Rui I'm stuck on the subject of getting different markers assigned per taxonomy term, how does that work?

Is the Openlayer Taxonomy module the way to go or not? The module doesn't really have a useful documentation... so would you please be so kind to help me out?

Kind regards,
Laurent

Submitted by nigel on

I have not yet used this module but from reading the description it does seem to be the best fit for what you are trying to implement. I'll have a bit of a play around when I get the chance to see if I can get this working with and without the Openlayers taxonomy module then follow that up with a tutorial.

Submitted by Rui (not verified) on

Hi Nigel,
hi Laurent,

sorry for my late answer, but I´ve had recently a lot of work to do. And I forgot to look here again. :)
The good thing, I got it to work in a different way from what you had adviced.
I made as many different OpenLayers Layers as needed Taxonomy Terms. Each as is own icon for the term.
Then I made an own OpenLayers Data Overlay for every Term similar the first OpenLayers Data Overlay you made, but now I filter per each term too. Imho I think I just duplicated the OpenLayers Data Overlay for each term and put the needed terms filter there.
On the Map side I added these layers and added a switcher too.
Now I can switch on the map not only to one term, as for many terms too.
As I meant in my question, now I could see all restaurants that are 2 and/or 3 stars, or only the ones that have 1 and/or 3 stars.

Sorry for my bad english, but it´s hard to explain this in words. :)
Thanks again for your great tutorials. Hope this helps others, too. :)

Submitted by nigel on

Hi Rui, Many thanks for that, I've implemented my own working prototype but it's based on the same method you prescribed, creating an Openlayers Data Overlay for each term, manually add the taxonomy term filter for each one and enabe them in the maps layers.

I still keep the original restaurant type, so when I click on the thai restaurants I'll get the number of stars for each map shown on the map, which is a little different from what you wanted but puts a nice spin on what you can do in Openlayers.

Thinking out aloud I'll to check to whether there's a way of selecting your stars in the same block instead using the layer switcher.

I should be recording the tutorial soon and will tweet once it's uploaded.

Submitted by Roel Damalerio (not verified) on

Great Tutorial, Thanks. i am new in drupal, i just want to ask if contextual filter i applicable also in content type: title? or just taxonomy. i just want to replace taxonomy term name to Content type : title. any help.

Submitted by Roel Damalerio (not verified) on

Thanks, i got it. i use to filter only one location. for searching of the specific location of a restaurant. one more question, how to replace base layer to shapefile?

Submitted by nigel on

try going to the openlayers section and selecting the layers tab and then cilck add for a new layer. Also have a search in drupal groups to see if other people have tried this.

Submitted by Carl (not verified) on

Hi,

Is there a way to create another view in a block to show let's say restaurant names of the taxonomy choice selection ?

Thanks Nigel for your pretty well explained Drupal examples. I always come to your site for pretty straightforward explanations.

Bye

Submitted by Carl (not verified) on

Thanks Nigel,

You solved two issues at the same time.
1. My Openlayers Data Overlay on my map view is not showing anything until i choose a restaurant type. Didn't know about the arg(1) argument. SOLVED.
2. The restaurant's name for the choosen 'restaurant type' now shows the name or title of the restaurant in another block view. I did the same addition in this block view by adding the argument and adding a relationsip based on the title. SOLVED.

Just great !

Submitted by nigel on
The arg(1) thing is standard procedure for contextual filtering on views for blocks.

Pages

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Recent Tweets

  • New post: Posting location co-ordinates to a drupal service - Part 2 http://t.co/MQhlVKBNKC 2 months 3 days ago
  • New post: Posting location co-ordinates to a drupal service - Part 1 http://t.co/2xlYgqFaJ0 2 months 4 days ago
  • New post: Connecting a drupal service with a mobile application - Part 2 http://t.co/01cFtAunDs 2 months 4 days ago
  • New post: Using drupal services to connect to mobile applications - Part 1 http://t.co/VEURftHCef 2 months 4 days ago
  • New post: Creating Drupal 7 video gallery http://t.co/HvpKU7uT 4 months 1 week ago