我想知道是否有人知道如何更新本教程(https://leafletjs.com/examples/choropleth/)以使用 react-leaflet v3 以及 react 函数组件。我无法工作的部分是当您将鼠标悬停在图层上时重置 GeoJSON 样式,以及在您单击图层时放大图层。这是我现在拥有的代码(我现在不太担心教程的实际着色部分)。另外,我一般对 react 或 JS 不太擅长,所以如果您发现任何不好的做法,请向我指出!谢谢!
import '../App.css';
import caMapData from '../data/caProvince.json'
import mxMapData from '../data/mxStates.json'
import usMapData from '../data/usStates.json'
import {MapContainer, TileLayer, Marker, Popup, GeoJSON} from 'react-leaflet'
function MainMap() {
const geoStyleCA = {
fillColor: 'white',
weight: 2,
opacity: 1,
color: 'red',
dashArray: '3',
fillOpacity: 0.5
}
const geoStyleUS = {
fillColor: 'white',
weight: 2,
opacity: 1,
color: 'blue',
dashArray: '3',
fillOpacity: 0.5
}
const geoStyleMX = {
fillColor: 'white',
weight: 2,
opacity: 1,
color: 'green',
dashArray: '3',
fillOpacity: 0.5
}
const onEachFeature = ((feature, layer) => {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
})
const highlightFeature = ((e) => {
e.target.bringToFront();
e.target.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
})
const resetHighlight = ((e) => {
// update this to work with react-leaflet 3 and functional react components
e.target.resetStyle();
})
const zoomToFeature = ((e) => {
// update this to work with react-leaflet 3 and functional react components
e.fitBounds(e.target.getBounds());
})
return(
<MapContainer
center={[44.967243, -103.771556]}
zoom={4}
scrollWheelZoom={true}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[44.967243, -103.771556]}>
<Popup>
Middle of US.
</Popup>
</Marker>
<GeoJSON data={caMapData.features} style={geoStyleCA}/>
<GeoJSON data={usMapData.features} style={geoStyleUS} onEachFeature={onEachFeature}/>
<GeoJSON data={mxMapData.features} style={geoStyleMX}/>
</MapContainer>
);
}
export default MainMap;