gpt4 book ai didi

javascript - 弹出react传单图库

转载 作者:行者123 更新时间:2023-12-02 21:19:43 24 4
gpt4 key购买 nike

我正在使用react-leaflet map 库https://react-leaflet.js.org/en/在我的 react 应用程序中,我在 map 上渲染了一些标记,当用户单击标记时,会出现一个弹出窗口。当用户单击我的 map 的区域时,我也想打开一个类似的弹出窗口。我怎样才能做到这一点?以下是我用弹出窗口渲染标记的代码。 ( map 使用geojson数据渲染)

markerHospitalRender() {
return this.props.hospitalData.map(item => {
const position = [item.district_lat, item.district_long];
return (
<Marker position={position} icon={this.grenIcon}>
<Popup>
<span style={{ display: "block" }}>{item.name}</span>
</Popup>
</Marker>
);
});
}

<Map
className="map"
center={center}
>
<GeoJSON
data={geo}
style={this.hospital_style}
/>
{this.markerHospitalRender()}
</Map>

最佳答案

使用onEachFeature支持react-leafletGeoJSON包装器传递类似于 native 传单的箭头函数 onEachFeature function单击每个区时能够生成弹出窗口。

<GeoJSON
data={geo}
style={this.hospital_style}
onEachFeature={onEachFeature}
/>

const onEachFeature = (feature, layer) => {
const popupContent = `District Code: ${feature.properties.electoralDistrictCode} <br> District: ${feature.properties.electoralDistrict}`;
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};

关于javascript - 弹出react传单图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60878939/

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