Exploring potentials in OpenLayers by filtering characteristics

Is there a way to filter map features based on their properties?

For example, if I have the following property in the geojson:

...
"properties": {
                "Start": 10
            }
...

How can I make it so that only features with Start > 10 are visible, while features with Start < 10 are hidden?

I've tried changing the style of the features to make them transparent, but they still remain clickable. Is there a way to completely hide features that don't meet the criteria?

let invisibleStyle = new ol.style.Fill({
    color: [0,0, 0, 0],
});

const NewLayer= new ol.layer.VectorImage ({
        source: new ol.source.Vector({
            url: *url*,
            format: new ol.format.GeoJSON(),
        }),
        visible: true,
        style: function (feature) {
        if (feature.get('Start')>10) {
            let style = new ol.style.Style({
                fill: fillStyle,
                stroke: strokeStyle
            })
            return style;   
        } else {
            let style = new ol.style.Style({
                fill: invisibleStyle,
            })
            return style;   
        }   
    });
map.addLayer(NewLayer);

I also attempted using the visible property like this, but it didn't work as expected:

const NewLayer= new ol.layer.VectorImage ({
        source: new ol.source.Vector({
            url: *url*,
            format: new ol.format.GeoJSON(),
        }),
        visible: function(feature) {
                 if (feature.get('Start')<10) {
                    return true;
                 } else {
                    return false;
                 }
          },
        style: new ol.style.Style({
              fill: fillStyle,
              stroke: strokeStyle,
              })      
    });
map.addLayer(NewLayer);

Answer №1

Invisible fill is applied for detecting clicks within a polygon without being visible. If you do not want it to be displayed or detected, simply do not include a style.

    style: function (feature) {
      if (feature.get('Start')>10) {
        let style = new ol.style.Style({
            fill: fillStyle,
            stroke: strokeStyle
        })
        return style;   
      }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for removing markers from personal Google Maps

I am having trouble deleting markers from my Google Maps using my code. The markers array seems to be empty even after adding markers. Can anyone help me troubleshoot this? Thank you! When I use console.log(markers.length) in the removeMarkers() function, ...

Presenting information extracted from the Meteor 1.0 framework

At the beginning of my Meteor application, I import data from a JSON file using the following code: 13 if (Meteor.isServer) { 14 15 //startup 16 Meteor.startup(function () { 17 if(Plugins.find().count() === 0) { 18 var plugins_data = J ...

Using data across multiple instances in node.js EJS partials

I need some advice regarding my nodejs project. I have created a sidebar partial that requires user data to be displayed on it. This sidebar is utilized across multiple pages in the project. However, currently, I have to include the user data in every func ...

React Application Issue: Unable to successfully redirect to the homepage upon logging in

Attempting to create a web application with the MERN stack is causing some trouble when trying to redirect users to the home page post-login. Here's how it should ideally function: User inputs login details The server validates these details and gene ...

Using ReactJS to incorporate events with an external DOM element

I am using ReactJS version 16.13.1 and I have the need to display an external DOM element along with its events. Let's consider having a <button type="button" id="testBtnSiri" onclick="alert('Functionality exists');">Testbutton Sir ...

Ensure the browser back button navigates to the login page seamlessly, without displaying any error

A scenario I am working on involves a Login jsp that accepts a user email and sends it to a servlet. If the provided email is not found in the database, the servlet redirects back to Login.jsp with an attribute "error". Within the header of Login.jsp, ther ...

Extracting specific key-value pairs from JSON data

Working with JSON data, I encountered a need to pass only specific key-value pairs as data to a component. Initially, I resorted to using the delete method to remove unwanted key-value pairs, which did the job but left me feeling unsatisfied. What I truly ...

Parent controller was not in command before Child执行

When working with a parent view and a child view, I encounter an issue with accessing a json file. In the parent view, I retrieve the json file using the following code: $scope.services = Services.query(); factory('Services', function($reso ...

Having trouble displaying images from the images folder in React

Currently working on a memory card game using React, but struggling to access the photos in the img folder from app.js. In my app.js file, I attempted to include the photos like so: Even though I have specified a URL for the pictures, they are not appear ...

What is the best way to embed two controllers within an AngularJS webpage?

Currently, I have a Web Forms ASP.NET website that I am trying to enhance by adding an AngularJS page. This page is meant to interact with my RESTful Web API to display quotes for selected securities upon button click. While the Web API calls work when dir ...

Controlling MVC controls dynamically using jQuery

I'm currently working on a table that contains multiple editable fields corresponding to an MVC model object. Each row has a cell with two buttons that toggle between edit and save functions when clicked. I've successfully implemented a mechanism ...

What could be causing the computed property in Vue 2 component to not return the expected state?

I'm encountering an issue with my Vue component where it fails to load due to one of its computed properties being undefined: Error: Cannot read properties of undefined (reading 'map') Here is the snippet of the computed property causing ...

Detecting the presence of a file on a local PC using JavaScript

In the process of creating a Django web application, I am exploring methods to determine if a particular application is installed on the user's local machine. One idea I have considered is checking for the existence of a specific folder, such as C:&bs ...

Styling elements with CSS

I've been attempting to align a button to the right of another button. The example above illustrates what I'm trying to achieve. I used Bootstrap to build it, which has a useful navbar feature for layout. However, despite my efforts to use right ...

Issue with box shadow appearing incorrectly as element content increases in size while the body has an image background

After applying a box shadow to the header div, I noticed that the box shadow doesn't display properly when the hidden elements within the header are revealed. <div id="header"> <div id="logo"> <a href="#"><img src="logo.png" ...

What is the reason for the PHP condition not functioning properly unless I explicitly echo the variable?

When the variable is echoed in the code below, the condition works, but if it's not echoed, the condition fails to work. What could be causing this issue? The original code snippet: $msg=$_SESSION['$msg']; echo $msg; if($msg != null){ ?> ...

Locally hosted website failing to transfer login details to external domain

Having trouble with an ajax call that is supposed to retrieve data from a web page, but instead returns a jQuery parse Error. Even though I can access the page directly, the ajax call doesn't seem to be working and storing the result properly. Below ...

Tips for locating the highest number in JavaScript

I'm having trouble with my code where the first number, even if it's the largest, is not displaying as such. Instead, it shows the second largest number. Even when following suggestions, I encountered an issue where if the numbers are entered as ...

Process JSON data from an input using Javascript

I am encountering an obstacle at the final step of my data flow process. Currently, I am in the midst of developing an application that retrieves input from an HTML form field and utilizes Ajax to fetch data relevant to the user's input. Allow me to e ...

Exploring the Origin of "props" in ReactJS

Currently, I am diving into the world of the official reactJS tutorial and you can find it here: https://reactjs.org/tutorial/tutorial.html#passing-data-through-props My journey with reactJS has been interesting so far, although there are still some parts ...