【发布时间】:2021-02-18 16:53:45
【问题描述】:
我有一张地图,其中显示了城市中每个不同公共设施的标记,我想要一组复选框来根据其类型过滤这些设施,一个类型复选框。每个类型都有多个标记,地图只会显示选中的标记。
我正在使用 react-leaflet v3。
这是我尝试做的:
<MapContainer center={[50,50]} zoom={13} scrollWheelZoom={true} whenCreated={setMap}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<LayersControl position='topright'>
{typologies.map((typology, index) => (
<LayersControl.Overlay key={index} checked name={typology}>
{publicFacilities.filter((publicFacility) => publicFacility.typology == typology ).map((publicFacility) => (
<Marker key={publicFacility._id} position={publicFacility.coordinates} />
))}
</LayersControl.Overlay>
))}
</LayersControl>
</MapContainer>
typologies 是字符串列表,publicFacilities 是对象列表。
但使用这种方法,我会为每个设施设置一个复选框。
【问题讨论】:
-
您的数据是什么样的?
typologies和publicFacilities. -
typologies是一个字符串列表,例如["office", "sports" ... ]和publicFacilities对象列表,例如[ { _id: "123", name:"town hall", typology:"office", coordinates:[50,50] }, ... ] -
我是working
-
如果你在
publicFacilities中添加了另一个typeology = "office" 的对象,那么它将出现两个名为office 的复选框,而我只想要一个用于所有办公室的复选框。 -
这是该问题的新信息。你应该在你的帖子中澄清这一点。但它会有相同的标记还是新的标记?如果它与第一个相比有不同的标记,你想用不同的复选框显示它们吗?这是您必须在问题描述中澄清的信息。
标签: javascript reactjs leaflet react-leaflet react-leaflet-v3