TransWikia.com

Purpose of filterEl.addEventListener('keyup', function(e)

Geographic Information Systems Asked by Guy Forssman on August 29, 2020

I’m learning the mapbox gl js examples and have trouble understanding a function.
The function can be found here https://www.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/ it’s in fact the “filterEl.addEventListener(‘keyup’, function(e)” in the example.

I already removed the complete function and noticed no difference in behaviour.

This is the code:

 filterEl.addEventListener('keyup', function(e) {
        var value = normalize(e.target.value);

        // Filter visible features that don't match the input value.
        var filtered = airports.filter(function(feature) {
            var name = normalize(feature.properties.name);
            var code = normalize(feature.properties.abbrev);
            return name.indexOf(value) > -1 || code.indexOf(value) > -1;
        });

        // Populate the sidebar with filtered results
        renderListings(filtered);

        // Set the filter to populate features into the layer.
        map.setFilter('airport', ['in', 'abbrev'].concat(filtered.map(function(feature) {
            return feature.properties.abbrev;
        })));
    });

One Answer

The function responds to keyboard input. It filters out airport names or airport codes from the list that don't contain each typed character, narrowing which airports are listed and which airport markers are displayed.

Answered by tbushman on August 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP