PaulLeCam / react-leaflet

React components for Leaflet maps
https://react-leaflet.js.org
Other
5.19k stars 887 forks source link

[Question] How to set default selection in LayerControl #1145

Open dansiviter opened 1 month ago

dansiviter commented 1 month ago

I'd like to know how to select the default layer within the LayerControl. Is seems this is a somewhat confusing thing outside of react so wonder if there is something I'm missing. I have the following:


const osm = (
    <TileLayer
      attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      zIndex={ -1 }
  />);

  return (
    <MapContainer>
      { osm }
      <LayersControl position="topright">
        <LayersControl.BaseLayer name="OpenStreetMap">
        { osm }
        </LayersControl.BaseLayer>
        <LayersControl.BaseLayer name="OpenStreetMap HOT">
          <TileLayer
            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a>'
            url="https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png"
          />
        </LayersControl.BaseLayer>
      </LayersControl>
 </MapContainer>
  )
}

But it doesn't select it: image

I've tried a few variants moving the TileLayer around but to no avail.