我在
注意:属性
ReactMapGL
下指定了两个<Source>
元素,尝试加载两个具有不同颜色的不同FeatureCollection
。当两个Source
同时存在时,它无法工作,因为它隐藏了第一个(id:maps-with-yield
)并显示后一个(id:maps-without-yield
)。
对于第一个Source
也打印出以下警告。
TypeError: Cannot read property 'fill-color' of undefined
我们不能使用多个来源吗?还是我在这里做错了什么?
<MapGL
{...viewport}
mapboxApiAccessToken={accessToken}
onViewportChange={viewport => setViewport(viewport)}
onHover={onHover}
onClick={onClick}
onLoad={onLoad}
width="100%"
height="100%"
scrollZoom={false}
dragRotate={false}
touchRotate={false}
keyboard={false}
>
{map && map.features.length > 0 ? (
<Source id="maps-with-yield" type="geojson" data={map}>
<Layer
id="data"
type="fill"
paint={{
'fill-color': {
property: 'yield',
stops: [
[minYield, worstYieldColor],
[maxYield, bestYieldColor]
]
},
'fill-outline-color': '#fff'
}}
/>
</Source>
) : null}
{mapWithoutYield && mapWithoutYield.features.length > 0 ? (
<Source id="maps-without-yield" type="geojson" data={mapWithoutYield}>
<Layer
id="data"
type="fill"
paint={{
'fill-color': '#66AEEC',
'fill-outline-color': '#fff'
}}
/>
</Source>
) : null}
</MapGL>
注意:属性
yield
总是可用的,因为我已经打印了 map
和 mapWithoutYield
数据集并进行了检查。