What is @react-leaflet/core?
@react-leaflet/core is a core library for integrating Leaflet maps with React. It provides a set of hooks and components that allow developers to create interactive maps with ease, leveraging the power of React's declarative approach.
What are @react-leaflet/core's main functionalities?
Map Container
This feature allows you to create a map container and add a tile layer to it. The MapContainer component initializes the map, and the TileLayer component adds the map tiles from OpenStreetMap.
import { MapContainer, TileLayer } from '@react-leaflet/core';
function MyMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
</MapContainer>
);
}
Markers
This feature allows you to add markers to the map. The Marker component places a marker at the specified position, and the Popup component displays a popup when the marker is clicked.
import { MapContainer, TileLayer, Marker, Popup } from '@react-leaflet/core';
function MyMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
<Marker position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
);
}
Polylines
This feature allows you to draw polylines on the map. The Polyline component takes an array of latitude and longitude pairs and draws a line connecting them.
import { MapContainer, TileLayer, Polyline } from '@react-leaflet/core';
function MyMap() {
const polyline = [
[51.505, -0.09],
[51.51, -0.1],
[51.51, -0.12]
];
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
<Polyline positions={polyline} />
</MapContainer>
);
}
Other packages similar to @react-leaflet/core
react-leaflet
react-leaflet is a popular library that provides React components for Leaflet maps. It offers a higher-level API compared to @react-leaflet/core, making it easier to use for common tasks. However, @react-leaflet/core provides more flexibility and control for advanced use cases.
leaflet
leaflet is the core JavaScript library for interactive maps. It is not specific to React and can be used with any JavaScript framework or vanilla JavaScript. @react-leaflet/core builds on top of leaflet to provide a React-friendly API.
react-map-gl
react-map-gl is a React wrapper for Mapbox GL JS, which provides interactive maps with WebGL. It offers similar functionalities to @react-leaflet/core but is based on Mapbox instead of Leaflet. It is suitable for applications that require high-performance rendering and advanced map features.