React Leaflet
Exposes Leaflet map layers as React components.
- Downloads / mo
- 23.0M
- Stars
- 5.6K
- Last release
- 5.0.0
- License
- Hippocratic-2.1
- Runs on
- Leaflet
Strengths
- Wraps familiar Leaflet layers. Use Leaflet layers through React components.
- Exposes Leaflet map instances. Access the Leaflet map through useMap.
Limitations
- Initial camera props stay fixed. Changing MapContainer props later does not update the existing map.
- Needs client-side loading. Load the map on the client in server-rendered applications.
Quickstart
import type { LatLngTuple } from 'leaflet' import React from 'react' import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet' const position: LatLngTuple = [51.505, -0.09,] export default function App() { return ( <MapContainer center={position} zoom={13} scrollWheelZoom={false}> <TileLayer attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <Marker position={position}> <Popup> A pretty CSS3 popup. <br /> </Popup> </Marker> </MapContainer> ) }
React Leaflet examples
const Map: React.FC<MapProps> = ({ center }) => { return ( <MapContainer center={(center as L.LatLngExpression) || [52, -0.09]} zoom={center ? 4 : 2} scrollWheelZoom={false} className={`h-full rounded-lg`} > <TileLayer attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> {center && <Marker position={center as L.LatLngExpression} />} </MapContainer> ); };
export default function TenThousandMarker() { return ( <div> <h2>10,000 Markers Example</h2> <MapContainer style={{ height: '500px' }} center={[-41.975762, 172.934298]} zoom={4} scrollWheelZoom={true} > <TileLayer attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <MarkerClusterGroup chunkedLoading> {(addressPoints as AddressPoint).map((address, index) => ( <Marker key={index} position={[address[0], address[1]]} title={address[2]} /> ))} </MarkerClusterGroup> </MapContainer> </div> ) }