React-leaflet、GeoJson:从 Cascader 选择选项后着色层出现问题

发布于 2025-01-15 13:12:39 字数 1530 浏览 0 评论 0原文

我的交互式地图有问题,我有一个带有 GeoJSON 图层的反应传单地图。另外,我添加了一些状态组件,例如带有一些国家名称选项的 Cascader,例如奥地利、比利时等......我想要这样的。级联状态完成后(因此,在级联中单击一个国家/地区后),我想更改该级联中所选国家/地区的颜色并将其显示在地图上。例如,如果我选择奥地利,我想在地图上为奥地利着色。另外,cascader 来自 Ant Desing 库。 selectedCountries 是另一个不重要的状态。

function onEachCountry(country, layer) {
    const countryName = country.properties.ADMIN

      if (currentValueCascader.includes(countryName)) {
        layer.setStyle({ fillColor: 'yellow', weight: 2, fillOpacity: 5 })
      } else {
        layer.setStyle({ color: "#000000", fillColor: "white", weight: 1 })
      }
}

return(
<Cascader
                showSearch
                style={{ width: 256 }}
                options={countries}
                onChange={(e) => {
                  setCurrentValueCascader(e)
                }}
                placeholder="Search country"
                value={currentValueCascader} 
                multiple
    
    
              /> 
<MapContainer
            style={{ height: "80vh", width: "auto" }}
            zoom={2.3}
            scrollWheelZoom={true}
            minZoom={2.3}
            center={[30, 0]}>
            <TileLayer url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' />
            <GeoJSON data={features} onEachFeature={(a, b) => { onEachCountry(a, b, selectedCountries) }} style={(a, b) => { onEachCountry(a, b, selectedCountries) }}}  />
          </MapContainer>
    )

我认为不知何故,Leaflet 地图没有重新渲染所选选项或 GeoJSON,但不知道如何解决它。

I have an issue with my interactive map, I have a react-leaflet map with GeoJSON layers. Also, I have added some state components like cascader with some name of countries options, like Austria, Belgium, etc... I want to have it like this. After the state from cascader is done (so after I click on a country in cascader), I want to change the color of the selected country from that cascader and display it on the map. For example, if I choose Austria, I want to have colored Austria on the map. Also, cascader is from the Ant Desing library. And selectedCountries is another state that is not important.

function onEachCountry(country, layer) {
    const countryName = country.properties.ADMIN

      if (currentValueCascader.includes(countryName)) {
        layer.setStyle({ fillColor: 'yellow', weight: 2, fillOpacity: 5 })
      } else {
        layer.setStyle({ color: "#000000", fillColor: "white", weight: 1 })
      }
}

return(
<Cascader
                showSearch
                style={{ width: 256 }}
                options={countries}
                onChange={(e) => {
                  setCurrentValueCascader(e)
                }}
                placeholder="Search country"
                value={currentValueCascader} 
                multiple
    
    
              /> 
<MapContainer
            style={{ height: "80vh", width: "auto" }}
            zoom={2.3}
            scrollWheelZoom={true}
            minZoom={2.3}
            center={[30, 0]}>
            <TileLayer url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' />
            <GeoJSON data={features} onEachFeature={(a, b) => { onEachCountry(a, b, selectedCountries) }} style={(a, b) => { onEachCountry(a, b, selectedCountries) }}}  />
          </MapContainer>
    )

I think that somehow the Leaflet map is not re-rendering that chosen option or maybe that GeoJSON, but don't know how to solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文