I need to create filters based on attributes of markers. I have markers defined this way:

var locations = [    
['AB11 5HW','17','A',57.147701,-2.085442 ] ,
['AB11 8DG','3','B',57.129372,-2.090916 ] ,

If the user selects for 'A' or 'B' all markers containing attribute A or B should become visible on the map. Something like

var markersA =  a[2] contains 'A' 

I thought it is maybe possible with Leaflet Marker groups or something similar.

I'm new to Javascript so I'm interested if it's even possible.


If you know the amount of attributes ahead of time, you can use Layers to get the job done. Leaflet has a good tutorial on that.

Basically, you loop through the initial array and split them into two separate arrays based on the specified variable. Then you add each array to it's own LayerGroup which gives you an easy way to turn the Markers on and off en-masse.

Here's a working jsfiddle showing how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.