Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
React Leaflet Bound not working as expected

I'm using react-leaflet package to create bound animation on the map from selected markers as seen on the screenshot below.

enter image description here

Here is the full code: https://codesandbox.io/s/react-leaflet-marker-with-bound-869mj

The map will be bound if only the selected markers change.

I try to implement the bound animation sample code from the documentation here https://react-leaflet.js.org/docs/example-view-bounds/

As we can see from the screenshot above, the map, the markers, the rectangle, and the panel (top-right corner) are displaying and working fine.

But if we change the selected markers (through the panel), the bound animation is not working properly (not showing all of the markers).

And if we empty the selected markers (no marker is selected in the panel), the app crashes and produces an error Cannot read properties of undefined (reading 'lat').

So, my questions are:

  1. how could this happen?
  2. what is the solution?
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The error Cannot read properties of undefined (reading 'lat') is because you are passing an empty array of markers to <Rectangle>. You need at least two markers to define a rectangles corners so check that you have at least two:

{/* RECTANGLE */}
{markerList && markerList.length >= 2 && (
  <Rectangle bounds={markerList} pathOptions={{ color: "red" }} />
)}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Finally, I could do it after directly updating the newest value of bounds (location list) inside onCheckboxChange function. So the useEffect hook with selectedPlaces is not needed anymore.

const onCheckboxChange = (inputIndex) => {
  let newPlaces = [...selectedPlaces];
  newPlaces[inputIndex].selected = !newPlaces[inputIndex].selected;
  setSelectedPlaces(newPlaces);

  let newBounds = selectedPlaces.filter((item) => item.selected);
  newBounds = newBounds.map((item) => item.location);
  setBounds(newBounds);
  map.fitBounds(newBounds);
};

useEffect(() => {
  map.fitBounds(initialBounds);
}, []);

Here is the working demo: https://codesandbox.io/s/learn-react-leaflet-cnk8tm

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda