Top Related Projects
🍃 JavaScript library for mobile-friendly interactive maps 🇺🇦
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
React friendly API wrapper around MapboxGL JS
React.js Google Maps integration component
A React binding of mapbox-gl-js
Quick Overview
React-Leaflet is a React wrapper for the popular Leaflet mapping library. It provides React components for Leaflet maps, allowing developers to easily integrate interactive maps into their React applications while leveraging the power and flexibility of Leaflet.
Pros
- Seamless integration with React applications
- Simplified API compared to vanilla Leaflet
- Automatic handling of map and layer lifecycle
- Good documentation and active community support
Cons
- Performance can be slower compared to vanilla Leaflet for complex maps
- Limited customization options for some advanced Leaflet features
- Learning curve for developers new to Leaflet concepts
- Occasional version compatibility issues with Leaflet
Code Examples
- Creating a basic map:
import { MapContainer, TileLayer } from 'react-leaflet'
function MyMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '400px' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
/>
</MapContainer>
)
}
- Adding a marker to the map:
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet'
function MapWithMarker() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '400px' }}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
)
}
- Using event handlers:
import { MapContainer, TileLayer, useMapEvents } from 'react-leaflet'
function LocationFinder() {
const map = useMapEvents({
click(e) {
console.log('Clicked at:', e.latlng)
map.flyTo(e.latlng, map.getZoom())
},
})
return null
}
function InteractiveMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '400px' }}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<LocationFinder />
</MapContainer>
)
}
Getting Started
-
Install the required packages:
npm install react react-dom leaflet react-leaflet
-
Import the necessary CSS in your main application file:
import 'leaflet/dist/leaflet.css'
-
Create a basic map component:
import { MapContainer, TileLayer } from 'react-leaflet' function MyMap() { return ( <MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '400px' }}> <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' /> </MapContainer> ) }
-
Use the map component in your React application:
import MyMap from './MyMap' function App() { return ( <div> <h1>My React-Leaflet Map</h1> <MyMap /> </div> ) }
Competitor Comparisons
🍃 JavaScript library for mobile-friendly interactive maps 🇺🇦
Pros of Leaflet
- More flexible and can be used with any JavaScript framework
- Lighter weight and potentially better performance
- Extensive plugin ecosystem
Cons of Leaflet
- Requires more setup and configuration for React projects
- Less React-specific optimizations and integrations
- May require additional wrappers or components for React state management
Code Comparison
Leaflet (vanilla JavaScript):
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
react-leaflet:
import { MapContainer, TileLayer } from 'react-leaflet';
function Map() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© OpenStreetMap contributors"
/>
</MapContainer>
);
}
The Leaflet repository provides the core mapping library, offering more flexibility and a broader ecosystem. However, react-leaflet simplifies integration with React applications, providing a more declarative API and better React-specific optimizations. The code comparison demonstrates how react-leaflet encapsulates Leaflet functionality in React components, making it easier to use within React projects.
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
Pros of mapbox-gl-js
- More powerful and feature-rich, offering advanced 3D mapping capabilities
- Better performance for large datasets and complex visualizations
- Extensive customization options for styling and interactivity
Cons of mapbox-gl-js
- Requires a Mapbox account and API key, which may incur costs for high usage
- Steeper learning curve due to its more complex API and features
- Less straightforward integration with React compared to react-leaflet
Code Comparison
mapbox-gl-js:
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-74.5, 40],
zoom: 9
});
react-leaflet:
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet'
<MapContainer center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker position={[51.505, -0.09]}>
<Popup>A sample marker</Popup>
</Marker>
</MapContainer>
Both libraries offer powerful mapping capabilities, but mapbox-gl-js provides more advanced features and better performance at the cost of a steeper learning curve and potential usage fees. react-leaflet, on the other hand, offers easier React integration and is free to use, but with more limited functionality and customization options.
React friendly API wrapper around MapboxGL JS
Pros of react-map-gl
- Built on Mapbox GL JS, offering high-performance vector maps and advanced features
- Extensive documentation and examples for various use cases
- Strong integration with other Uber Vis.gl libraries for data visualization
Cons of react-map-gl
- Requires a Mapbox access token, which may incur costs for high-volume usage
- Steeper learning curve due to more complex API and features
Code Comparison
react-map-gl:
import Map from 'react-map-gl';
<Map
initialViewState={{
longitude: -100,
latitude: 40,
zoom: 3.5
}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v9"
/>
react-leaflet:
import { MapContainer, TileLayer } from 'react-leaflet';
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: 400 }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</MapContainer>
Both libraries offer React components for interactive maps, but react-map-gl provides more advanced features and performance at the cost of complexity and potential usage fees. react-leaflet is simpler to use and relies on free map tiles by default, making it a good choice for basic mapping needs.
React.js Google Maps integration component
Pros of react-google-maps
- More comprehensive set of Google Maps features and components
- Better integration with Google Maps API, including advanced functionalities
- Larger community and more frequent updates
Cons of react-google-maps
- Steeper learning curve due to more complex API
- Requires a Google Maps API key, which may incur costs for high-usage applications
- Heavier bundle size compared to react-leaflet
Code Comparison
react-google-maps:
import { GoogleMap, Marker } from '@react-google-maps/api';
<GoogleMap
center={{ lat: 40.7128, lng: -74.0060 }}
zoom={10}
>
<Marker position={{ lat: 40.7128, lng: -74.0060 }} />
</GoogleMap>
react-leaflet:
import { MapContainer, TileLayer, Marker } from 'react-leaflet';
<MapContainer center={[40.7128, -74.0060]} zoom={10}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker position={[40.7128, -74.0060]} />
</MapContainer>
Both libraries offer React components for map integration, but react-google-maps provides more Google-specific features, while react-leaflet offers a simpler API and is based on the open-source Leaflet library. The choice between them depends on specific project requirements, budget constraints, and desired map features.
A React binding of mapbox-gl-js
Pros of react-mapbox-gl
- Better performance for large datasets and complex visualizations
- More advanced 3D mapping capabilities
- Extensive customization options for map styles and interactions
Cons of react-mapbox-gl
- Requires a Mapbox account and API key, which may incur costs
- Steeper learning curve due to more complex API
- Less extensive documentation compared to react-leaflet
Code Comparison
react-mapbox-gl:
import ReactMapGL from 'react-map-gl';
<ReactMapGL
mapboxApiAccessToken={MAPBOX_TOKEN}
width="100%"
height="400px"
latitude={37.7577}
longitude={-122.4376}
zoom={8}
/>
react-leaflet:
import { MapContainer, TileLayer } from 'react-leaflet';
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '400px' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</MapContainer>
Both libraries offer React components for creating interactive maps, but react-mapbox-gl provides more advanced features and better performance at the cost of complexity and potential fees. react-leaflet is simpler to use and free but may lack some advanced capabilities for complex mapping needs.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
React Leaflet
React components for Leaflet maps.
Documentation
Changes
See the CHANGELOG file.
Contributing
See the CONTRIBUTING file.
Support
Please do not use GitHub issues for support, but instead post your questions on StackOverflow using the react-leaflet
tag.
License
Hippocratic License - see the LICENSE file.
Top Related Projects
🍃 JavaScript library for mobile-friendly interactive maps 🇺🇦
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
React friendly API wrapper around MapboxGL JS
React.js Google Maps integration component
A React binding of mapbox-gl-js
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot