You need something like:

...
const mapRef = useRef()

...
<MapGL
    ref={mapRef}
    onClick={e => {
        const features = mapRef.current.queryRenderedFeatures(e.geometry.coordinates, { layers: ['ramps'] })
        console.log(features)
    }}
    ...
/>
Answer from greenafrican on Stack Overflow
🌐
Reddit
reddit.com › r/reactjs › react-map-gl // how to display markers based on a filter
r/reactjs on Reddit: React-map-gl // How to display markers based on a filter
October 14, 2020 -

Hello everyone!

React newbie here. I'm seeking your help because I am currently struggling with a feature. I managed to display a list of markers from an external file (list of 3 museums in London) and added popups to them. But now I'd like to filter them with 3 buttons: let's say button1 displays marker 1, etc.

I have no clue on how to perform such thing: my first thought would be to add some specific characteristic to each of them in my Data.js file but then I do not know how to retrieve this info and based my buttons on this filter in my main component, could you please tell me how to do it? Any hints?

Here is what I've done so far:

// Main document
import React, {PureComponent} from "react";
import ReactDOM from "react-dom";
import ReactMapGL, {Marker, Popup} from "react-map-gl";
import "./index.css";
import Data from "./Data.js";


const MAPBOX_TOKEN = "Mapbox_token";


class Mapp extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            viewport: {
                latitude: 51.50985,
                longitude: -0.11892,
                zoom: 12,
            },
            markers: Data,
            selectedMarker: null
        };
    }

    _renderMarkers() {
        return (
            this.state.markers.map(marker => (
                <Marker
                    key={marker.id}
                    longitude={marker.longitude}
                    latitude={marker.latitude}>
                        <button 
                            className="marker-btn"
                            onClick={event => {
                                event.preventDefault();
                                this.setState({selectedMarker: marker})
                            }}>
                            <img 
                                className="marker-btn"
                                src="location-icon.svg"
                                alt="location Icon"
                            />
                        </button>
                </Marker>
            ))
        )
    }

    _renderPopup() {
        return(
            this.state.selectedMarker && (
                <Popup
                    longitude={this.state.selectedMarker.longitude}
                    latitude={this.state.selectedMarker.latitude}
                    onClose={() => {this.setState({selectedMarker: null})}}
                    offsetLeft={22.5}
                >
                    <div>
                        <h3>Name: {this.state.selectedMarker.site_name}</h3>
                        <h4>Website: {this.state.selectedMarker.website}</h4>
                    </div>
                </Popup>
            )
        )
    }

    render() {
        return (
            <div className="App">
                <div className="Map">
                    <ReactMapGL
                        {...this.state.viewport}
                            width="100vw"
                            height="100vh"
                            mapStyle="mapbox://styles/mapbox/streets-v11"
                            onViewportChange={viewport => this.setState({viewport})}
                            mapboxApiAccessToken={MAPBOX_TOKEN}
                        >

                            {this._renderMarkers()}
                            {this._renderPopup()}

                    </ReactMapGL>
                </div>
            </div>
        );
    }
}

document.body.style.margin = 0;
ReactDOM.render(
    <Mapp />, 
    document.getElementById("root")
)

The file containing the information of my markers:

//Data.js
const Data = [
  {
    "id": 1,
    "latitude": 51.5206436238135,
    "longitude": -0.12869802265875,
    "site_name": "Museum of Writing",
    "website": "http://www.museumofwriting.co.uk/"
  },
  {
    "id": 2,
    "latitude": 51.5252701073731,
    "longitude": -0.1216462226263,
    "site_name": "The Foundling Museum",
    "website": "http://www.foundlingmuseum.org.uk/"
  },
  {
    "id": 3,
    "latitude": 51.5484218889309,
    "longitude": -0.17724635343509,
    "site_name": "Freud Museum",
    "website": "https://www.freud.org.uk/"
  }
]

export default Data

Thank you very much in advance !!

Discussions

react native mapbox gl - MapboxGL.SymbolLayer filter expression to check if id in array - Stack Overflow
Any idea how to solve this with mapbox filter expressions? ... See https://github.com/react-native-mapbox-gl/maps/issues/70#issuecomment-499775185 in is a legacy filter syntax and not supported. More on stackoverflow.com
🌐 stackoverflow.com
React-map-gl // How to filter with buttons and display markers
Hey you could alter the state ( markers ) using the buttons. Eg. remove marker 2 and 3 when you press button 1. Your button would trigger a function which alters this.state.markers. with this.setState. Hope this helps :) More on reddit.com
🌐 r/reactjs
6
2
October 17, 2020
Configuring source, source-layer and filter propertites
I can't tell from the examples ... Mapbox GL JS library directly: map.addLayer({ "id": "my-id", "type": "fill", "source": "myMapboxHostedTileSetSource", "source-layer": "victoriaAustralia", "layout": {}, "paint": { "fill-color": "#81D8D0", "fill-opacity": 0.5 }, "filter": ["==", ... More on github.com
🌐 github.com
12
June 10, 2016
Newest 'react-map-gl' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
🌐
CodeSandbox
codesandbox.io › s › reactmapglgeocoder-filter-example-602xq
react-map-gl-geocoder filter example - CodeSandbox
February 24, 2021 - \nExample usage of react-map-gl-geocoder which is a React wrapper for mapbox-gl-geocoder for use with react-map-gl
Published   May 24, 2019
Author   SamSamskies
🌐
GitHub
github.com › urbica › react-map-gl
GitHub - urbica/react-map-gl: React Component Library for Mapbox GL JS · GitHub
Set filter to existing layer · CustomLayer · Allow a user to render directly into the map's GL context · Image · Adds an image to the map style · Popup · React Component for Mapbox GL JS Popup · Marker · React Component for Mapbox GL JS Marker · FeatureState ·
Starred by 424 users
Forked by 49 users
Languages   JavaScript
🌐
Sparkgeo
sparkgeo.com › home › use flatgeobuf in react-map-gl
Use Flatgeobuf in react-map-gl | Sparkgeo
May 4, 2023 - In one example from their website, ... used to filter a large dataset by bounding box. This example shows how you can use it to stream large datasets without the need for anything more complex than a file server in its support. In this post, we expand on that example by providing a working extension to the Source component in react-map-gl that manages ...
🌐
React Map GL
visgl.github.io › layer
Layer | react-map-gl
import * as React from 'react'; import {Map, Layer} from 'react-map-gl/maplibre'; import type {FillLayer} from 'react-map-gl/maplibre'; import 'maplibre-gl/dist/maplibre-gl.css'; const parkLayer: FillLayer = { id: 'landuse_park', type: 'fill', source: 'vector', 'source-layer': 'landuse', filter: ['==', 'class', 'park'], paint: { 'fill-color': '#4E3FC8' } }; function App() { return <Map initialViewState={{ longitude: -122.4, latitude: 37.8, zoom: 14 }} mapStyle="https://demotiles.maplibre.org/style.json" > <Layer {...parkLayer} /> </Map>; }
🌐
Reddit
reddit.com › r/reactjs › react-map-gl // how to filter with buttons and display markers
r/reactjs on Reddit: React-map-gl // How to filter with buttons and display markers
October 17, 2020 -

Hello everyone!

React newbie here. I'm seeking your help because I am currently struggling with a feature. I managed to display a list of markers from an external file (list of 3 museums in London) and added popups to them. But now I'd like to filter them with 3 buttons: let's say button1 displays marker 1, etc.

I have no clue on how to perform such thing: my first thought would be to add some specific characteristic to each of them in my Data.js
file but then I do not know how to retrieve this info and based my buttons on this filter in my main component, could you please tell me how to do it? Any hints?

Here is what I've done so far:

// Main document
import React, {PureComponent} from "react";
import ReactDOM from "react-dom";
import ReactMapGL, {Marker, Popup} from "react-map-gl";
import "./index.css";
import Data from "./Data.js";


const MAPBOX_TOKEN = "Mapbox_token";


class Mapp extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            viewport: {
                latitude: 51.50985,
                longitude: -0.11892,
                zoom: 12,
            },
            markers: Data,
            selectedMarker: null
        };
    }

    _renderMarkers() {
        return (
            this.state.markers.map(marker => (
                <Marker
                    key={marker.id}
                    longitude={marker.longitude}
                    latitude={marker.latitude}>
                        <button 
                            className="marker-btn"
                            onClick={event => {
                                event.preventDefault();
                                this.setState({selectedMarker: marker})
                            }}>
                            <img 
                                className="marker-btn"
                                src="location-icon.svg"
                                alt="location Icon"
                            />
                        </button>
                </Marker>
            ))
        )
    }

    _renderPopup() {
        return(
            this.state.selectedMarker && (
                <Popup
                    longitude={this.state.selectedMarker.longitude}
                    latitude={this.state.selectedMarker.latitude}
                    onClose={() => {this.setState({selectedMarker: null})}}
                    offsetLeft={22.5}
                >
                    <div>
                        <h3>Name: {this.state.selectedMarker.site_name}</h3>
                        <h4>Website: {this.state.selectedMarker.website}</h4>
                    </div>
                </Popup>
            )
        )
    }

    render() {
        return (
            <div className="App">
                <div className="Map">
                    <ReactMapGL
                        {...this.state.viewport}
                            width="100vw"
                            height="100vh"
                            mapStyle="mapbox://styles/mapbox/streets-v11"
                            onViewportChange={viewport => this.setState({viewport})}
                            mapboxApiAccessToken={MAPBOX_TOKEN}
                        >

                            {this._renderMarkers()}
                            {this._renderPopup()}

                    </ReactMapGL>
                </div>
            </div>
        );
    }
}

document.body.style.margin = 0;
ReactDOM.render(
    <Mapp />, 
    document.getElementById("root")
)

The file containing the information about my markers:

//Data.js
const Data = [
  {
    "id": 1,
    "latitude": 51.5206436238135,
    "longitude": -0.12869802265875,
    "site_name": "Museum of Writing",
    "website": "http://www.museumofwriting.co.uk/"
  },
  {
    "id": 2,
    "latitude": 51.5252701073731,
    "longitude": -0.1216462226263,
    "site_name": "The Foundling Museum",
    "website": "http://www.foundlingmuseum.org.uk/"
  },
  {
    "id": 3,
    "latitude": 51.5484218889309,
    "longitude": -0.17724635343509,
    "site_name": "Freud Museum",
    "website": "https://www.freud.org.uk/"
  }
]

export default Data

Thak you in advance! Cheeeeeers

Find elsewhere
🌐
GitHub
github.com › alex3165 › react-mapbox-gl › issues › 22
Configuring source, source-layer and filter propertites · Issue #22 · alex3165/react-mapbox-gl
June 10, 2016 - I can't tell from the examples ... Mapbox GL JS library directly: map.addLayer({ "id": "my-id", "type": "fill", "source": "myMapboxHostedTileSetSource", "source-layer": "victoriaAustralia", "layout": {}, "paint": { "fill-color": "#81D8D0", "fill-opacity": 0.5 }, "filter": ["==", ...
Author   charliedotau
🌐
Stack Overflow
stackoverflow.com › questions › tagged › react-map-gl
Newest 'react-map-gl' Questions - Stack Overflow
Filter · 0 votes · 2 answers · 166 views · I want to be able to show symbols on transitive-lines in a map application I'm writing with otp-ui (Open Trip Planner UI). Regardless of the zoom level, I want these bubbles containing transit-line ... mapbox-gl · react-map-gl ·
🌐
GitHub
github.com › urbica › react-map-gl › issues › 304
Add dynamic filter to GeoJSON source · Issue #304 · urbica/react-map-gl
August 25, 2020 - Is your feature request related to a problem? Please describe. I'm using clustering on a GeoJSON source. Until recently it was very hard to filter a cluster layer. But now Mapbox included filte...
Author   KaiHuebner
🌐
npm
npmjs.com › package › react-map-gl
react-map-gl - npm
3 weeks ago - React components for MapLibre GL JS and Mapbox GL JS. Latest version: 8.1.1, last published: 19 days ago. Start using react-map-gl in your project by running `npm i react-map-gl`. There are 482 other projects in the npm registry using react-map-gl.
      » npm install react-map-gl
    
Published   Apr 11, 2026
Version   8.1.1
🌐
React Map GL
visgl.github.io › react-map-gl
Home | react-map-gl
Comes with additional React interfaces such as context and hooks to support custom components. Use together with e.g. deck.gl to render performant and compelling 2D and 3D WebGL visualizations on top of your MapLibre GL JS and Mapbox GL JS based maps.
🌐
Stack Overflow
stackoverflow.com › tags › react-map-gl › hot
Hottest 'react-map-gl' Answers - Stack Overflow
Follow the suggestions here: https://github.com/mapbox/mapbox-gl-js/issues/10173 It's also in the official documentation ... ... Explanation Let's pull apart this error: Overload 1 of 2, '(props: SourceProps | Readonly<SourceProps>): Source' ... Overload 2 of 2, '(props: SourceProps, context: any): Source At the top ... ... I spent half a day figuring this out. The react-map-gl really needs to update its documentation.
🌐
React Map GL
visgl.github.io › adding custom data
Adding Custom Data | react-map-gl
import * as React from 'react'; import Map, {Source, Layer} from 'react-map-gl/maplibre'; import type {CircleLayer} from 'react-map-gl/maplibre'; import type {FeatureCollection} from 'geojson'; const geojson: FeatureCollection = { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'Point', coordinates: [-122.4, 37.8] }, properties: {title: '915 Front Street, San Francisco, California'} } ] }; const layerStyle: CircleLayer = { id: 'point', type: 'circle', paint: { 'circle-radius': 10, 'circle-color': '#007cbf' } }; function App() { return ( <Map initialViewState={{ longitude: -122.45, latitude: 37.78, zoom: 14 }}> <Source id="my-data" type="geojson" data={geojson}> <Layer {...layerStyle} /> </Source> </Map> ); }
🌐
GitHub
github.com › visgl › react-map-gl › discussions › 1575
Is there really no filter for features? · visgl/react-map-gl · Discussion #1575
I have looked everywhere and can't find a single site using react map gl where you are able to filter the features. What i currently have is just a bunch of clusters with some unclustered points in...
Author   visgl
🌐
GitHub
github.com › visgl › react-map-gl
GitHub - visgl/react-map-gl: React friendly API wrapper around MapboxGL JS · GitHub
react-map-gl is a suite of React components designed to provide a React API for mapbox-gl or maplibre-gl.
Starred by 8.4K users
Forked by 1.4K users
Languages   TypeScript 65.5% | JavaScript 32.2%
🌐
npm
npmjs.com › package › react-map-gl-geocoder
react-map-gl-geocoder - npm
August 7, 2021 - import 'mapbox-gl/dist/mapbox-gl.css' import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css' import React, { useState, useRef, useCallback } from 'react' import MapGL from 'react-map-gl' import Geocoder from 'react-map-gl-geocoder' // Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens const MAPBOX_TOKEN = 'REPLACE_WITH_YOUR_MAPBOX_TOKEN' const Example = () => { const [viewport, setViewport] = useState({ latitude: 37.7577, longitude: -122.4376, zoom: 8, }); const geocoderContainerRef = useRef(); const mapRef = useRef(); const ha
      » npm install react-map-gl-geocoder
    
Published   Aug 07, 2021
Version   2.2.0
Author   Sam Samskies
🌐
Lostcreekdesigns
lostcreekdesigns.co › writing › a-complete-guide-to-sources-and-layers-in-react-and-mapbox-gl-js
A Complete Guide to Sources and Layers in React and Mapbox GL JS | Lost Creek Designs
February 22, 2021 - The aim of this post is to provide an introduction to adding a variety of spatial data formats to a React application using Mapbox GL JS. Understanding how to add sources and layers to a map will open a lot of doors for the types of applications you can build using Mapbox GL JS.