Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Trouble figuring out how to use GeometryUtil with React-Leaflet

I'm building an application with JavaScript, React & Leaflet(+React-Leaflet plugin). I want to get coordinates of closest marker to my location, but I'm having trouble figuring out how to use GeometryUtil plugin with React-Leaflet.

I have tried this code, but it returns an error message.

const Map = ({ markers, center, zoom }) => {

/* My location. (Dummy data). */
const myLocation = [-40.5512, 69.9454];

/* Saving map instance to state: */
const [map, setMap] = useState(null);

*/ Using GeometryUtil. Error occurs here. */
let closest_latlng = L.GeometryUtil.closest(map, markers, myLocation);
console.log(closest_latlng);

return (
<MapContainer
  className="map"
  center={center}
  zoom={zoom}
  scrollWheelZoom={false}
  whenCreated={(map) => setMap(map)}
>
  <TileLayer
    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
  />
  {markers && (
    <>
      {markers.map((marker, idx) => (
        <AllMarkers key={idx} marker={marker} />
      ))}
    </>
  )}
</MapContainer>
);
};

export default Map;

Error message: Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'closest').

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is in the way you're importing the geometryutil package. Try like this:

import 'leaflet-geometryutil';

And then you should have L.GeometryUtil available anywhere L is available. Make sure this import happens at some root level file, like index.js or app.js, so that by the time you call L.GeometryUtil functions, it has already been imported.

Alternatively, in the file you want to use it in,

import GeometryUtil from "leaflet-geometryutil";
// or
import * as GeometryUtil from "leaflet-geometryutil";

let closest_latlng = GeometryUtil.closest(map, markers, myLocation); // no L.GeometryUtil
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda