gpt4 book ai didi

javascript - 尝试使用 react-leaflet-choropleth : Cannot read property 'map' of undefined 映射等值线时出错

转载 作者:行者123 更新时间:2023-12-05 06:07:40 25 4
gpt4 key购买 nike

我正在尝试使用 react-leaflet-choroplethreact 中制作等值线。

我的传单工作正常。
现在当我尝试加载 geojson 时(crimes_by_district) 使用库时,我在通过功能进行映射时遇到错误,具体而言:

Cannot read property 'map' of undefined

看来我没有准确地引用正确的数组进行映射。

我看过 issues在 git repo 中尝试了这些建议,但没有成功。我想知道我是否引用了我正在使用的 geoJson 中的错误值。

下面是我的代码:

Leaf.js(在 App.js 中呈现)

import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import classes from './leaf.module.css'
import Choropleth from 'react-leaflet-choropleth'
import geojson from "./assets/crimes_by_district.geojson";

const style = {
fillColor: '#F28F3B',
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.5
};

class Leaf extends Component {
render() {
return (
<Map>
<Choropleth
data= {{type: 'FeatureCollection', features: geojson.features}}
valueProperty={(feature) => feature.properties.value}
// visible={(feature) => feature.id !== active.id}
scale={['#b3cde0', '#011f4b']}
steps={7}
mode='e'
style={style}
onEachFeature={
(feature, layer) => layer.bindPopup(feature.properties.label)
}
ref={(el) => this.choropleth = el.leafletElement}
/>
</Map>
);
}
}

export default Leaf;

最佳答案

使用与您包含的扩展类似的相同等值库创建您自己的 choropleth 包装器,因为 react-leaflet-choropleth 似乎已过时:

function Choropleth() {
const { map } = useLeaflet();

useEffect(() => {
fetch(
"https://raw.githubusercontent.com/timwis/leaflet-choropleth/gh-pages/examples/basic/crimes_by_district.geojson"
)
.then((response) => response.json())
.then((geojson) => {
L.choropleth(geojson, {
valueProperty: "incidents", // which property in the features to use
scale: ["white", "red"], // chroma.js scale - include as many as you like
steps: 5, // number of breaks or steps in range
mode: "q", // q for quantile, e for equidistant, k for k-means
style,
onEachFeature: function (feature, layer) {
layer.bindPopup(
"District " +
feature.properties.dist_num +
"<br>" +
feature.properties.incidents.toLocaleString() +
" incidents"
);
}
}).addTo(map);
});
}, []);

return null;
}

然后将其作为 Map 子项导入:

<Map center={position} zoom={11} style={{ height: "100vh" }}>
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Choropleth />
</Map>

您可以通过传递您需要的各种变量作为 Prop 来进一步扩展它。

Demo

关于javascript - 尝试使用 react-leaflet-choropleth : Cannot read property 'map' of undefined 映射等值线时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65361496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com