import React, { useEffect } from 'react';
import { useMap } from 'react-leaflet';
import L from 'leaflet'; // Import Leaflet
const ColorRangeLegend = ({ colors, labels }: { colors: string[], labels: string[] }) => {
const map = useMap();
useEffect(() => {
// Add the legend to the map
// Create a custom control for the legend
const legend = new L.Control({ position: 'bottomright' });
legend.onAdd = () => {
const div = L.DomUtil.create('div', 'info legend');
div.style.backgroundColor = 'white';
div.style.padding = '10px';
div.style.border = '2px solid rgba(0,0,0,0.2)';
div.style.borderRadius = '5px';
// Loop through the colors and labels to create the legend
colors.forEach((color, index) => {
div.innerHTML += `
<div style="display: flex; align-items: center; margin-bottom: 5px;">
<div style="width: 20px; height: 20px; background-color: ${color}; margin-right: 10px;"></div>
<span>${labels[index]}</span>
</div>
`;
});
return div;
};
// Add the legend to the map
legend.addTo(map);
return () => {
map.removeControl(legend);
};
}, [map, colors, labels]);
return null;
};
export default ColorRangeLegend;
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]