If we assume that you have the district names on your dropdown and are equal with the names that are included inside the geojson, also the map instance, inside select
's onChange
you will have to use the json ref to find the layer with the selected district name and then perform any operation on it like open its popup or zoom to the selected layer.
const geoJsonRef = useRef();
const [selectValue, setSelectValue] = useState("");
...
const handleDistrictChange = (e) => {
const newDistrict = e.target.value;
setSelectValue(newDistrict);
if (!newDistrict) return;
const layer = geoJsonRef.current
.getLayers()
.find((layer) => layer._popup._content === newDistrict);
layer.openPopup();
map.fitBounds(layer.getBounds());
};
and add the ref on your GeoJSON component as well as getting the map instance from MapContainer.
<>
<MapContainer
center={position}
zoom={13}
style={{ height: "80vh" }}
whenCreated={setMap}
>
...
{geoJSON && (
<GeoJSON
data={geoJSON}
onEachFeature={handleEachFeature}
ref={geoJsonRef}
/>
)}
</MapContainer>
<select value={selectValue} onChange={handleDistrictChange}>
<option value="">Select a district</option>
{geoJsonRef.current
?.getLayers()
.map((layer) => layer._popup._content)
.map((district, index) => (
<option key={`district-${index}`} value={district}>
{district}
</option>
))}
</select>
</>
You can see more on this demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…