0

I'm using react Leaflet js map, and I'm displaying markers of fetched data from my database on the map, but the issue is that I have kind of 5000 markers and it's a lot to display on zoom level 9, so is there some way to display the markers on specific zoom levels?

1
  • Use react-leaflet-markercluster library Commented Dec 16, 2021 at 12:10

1 Answer 1

3

yes why not?

you can store zoom of map like this and set a condition to show Marker

const [Zoom, setZoom] = useState(9);

console.log(Zoom);

const MapEvents = () => {
  useMapEvents({
    zoomend() { // zoom event (when zoom animation ended)
      const zoom = map.getZoom(); // get current Zoom of map
      setZoom(zoom);
    },
  });
  return false;
};

return (
  <MapContainer center={[33.8735578, 35.86379]} zoom={9} scrollWheelZoom={true}>
    <TileLayer
      attribution='&copy; <a 
         href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />

    {/* your events on map */}
    <MapEvents /> 

    { Zoom >= 13 ? <Marker> your marker options and params </Marker> : null }

  </MapContainer>
);

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