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='© <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').