admin管理员组文章数量:1334807
I'm using react-leaflet and want to add a legend to the map.
I can currently do it by passing a ref of the map to a custom ponent and render null while having a function create the HTML and add it to the map. The refs are being a nightmare to test using Jest and Enzyme.
Is there a way to using a react-leaflet ponent to acplish this?
I'm using react-leaflet and want to add a legend to the map.
I can currently do it by passing a ref of the map to a custom ponent and render null while having a function create the HTML and add it to the map. The refs are being a nightmare to test using Jest and Enzyme.
Is there a way to using a react-leaflet ponent to acplish this?
Share Improve this question asked Aug 7, 2019 at 23:36 bhagwansbhagwans 1631 silver badge10 bronze badges1 Answer
Reset to default 9You can achieve that by extending react-leaflet
's MapControl
class and using for instance the legend code provided by Leaflet official page tutorial for Using GeoJSON with Leaflet.
class Legend extends MapControl {
ponentDidMount() {
// place your code here
...
const legend = L.control({ position: "bottomright" });
...
const { map } = this.props.leaflet;
legend.addTo(map);
}
}
and then import it here:
<Map style={{ height: "100vh" }} center={position} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap/{z}/{x}/{y}.png"
attribution='© <a href="http://osm/copyright">OpenStreetMap</a> contributors'
/>
<Legend />
</Map>
Demo
本文标签:
版权声明:本文标题:javascript - How to add a legend to the map using react leaflet, without using refs and manually modifying the DOM? - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742159243a2424632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论