Skip to content
Advertisement

Google Maps Filter Javascript

Looking for a way to filter the map markers using pure javascript based upon multiple conditions if they exist. This is based upon having potentially 4 potential filters possible.

So imagine I have two or more field sets

  • Project Type (this will be an array of project types [‘engineering’,’construction’])
  • Region (this will be an array of regions [‘France’,’Italy’])
  • Name (this will be string input ‘google’)
  • Value (integer input ‘1000000’)

I want to filter through the markers and then include those with matching criteria. And exclude the matching criteria if it doesn’t exist or have a value set.

Example1: engineering, france, google, 1000000 Example2: construction, italy Example3: engineering, france, 1000000

Trying to achieve this in an elegant manner and not get into a nested if statement pit of hell.

Here is a working example I have setup, which accounts for two filters but isn’t as clean as I would like and allow for the inclusion of extra filters easily.

Looking to have it accurately show the information based upon the users selections. So if they choose multiple projects then show them but if they are include a region and a value show where the project type, location and other match.

JavaScript

Example of projectData JSON

JavaScript

Advertisement

Answer

Given the lack of HTML, the minimal dataset and slightly fuzzy requirements the following might ( or might not ) be the sort of thing you are looking for. I have used this approach before to filter markers on a map where the dataset was much, much larger and the number of criteria was also much larger.

The filtering of markers is handled within the changehandler function – the comments in the code will help it make sense. You should be able to adapt the logic to checkboxes

JavaScript

Proof of concept

Advertisement