Top Related Projects
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
WebGL2 powered visualization framework
π JavaScript library for mobile-friendly interactive maps πΊπ¦
OpenLayers
MapLibre GL JS - Interactive vector tile maps in the browser
Vector and raster maps with GL styles. Server side rendering by MapLibre GL Native. Map tile server for MapLibre GL JS, Android, iOS, Leaflet, OpenLayers, GIS via WMTS, etc.
Quick Overview
The react-map-gl
library is a React wrapper for the popular Mapbox GL JS library, which provides a highly customizable and interactive mapping experience for web applications. It allows developers to easily integrate maps into their React-based projects, with a focus on performance, flexibility, and ease of use.
Pros
- Seamless Integration with React: The library is designed to work seamlessly with React, allowing developers to leverage the power of React's component-based architecture and state management.
- Customizable Mapping Experience:
react-map-gl
provides a wide range of customization options, enabling developers to tailor the map to their specific needs, including the ability to add custom layers, markers, and overlays. - Performance Optimization: The library is optimized for performance, with features like automatic viewport management and efficient rendering, ensuring a smooth user experience.
- Extensive Documentation and Community Support: The project has a well-maintained documentation and a thriving community of developers, providing ample resources for learning and troubleshooting.
Cons
- Dependency on Mapbox GL JS:
react-map-gl
is heavily dependent on the Mapbox GL JS library, which may be a concern for developers who prefer to use alternative mapping solutions or have concerns about Mapbox's licensing and pricing model. - Steep Learning Curve: While the library is designed to be user-friendly, the complexity of the Mapbox GL JS library and the integration with React can still present a learning curve for developers new to the ecosystem.
- Limited Offline Support: The library is primarily designed for online use, and offline functionality may require additional effort or third-party libraries.
- Potential Performance Issues with Large Datasets: Rendering large datasets on the map can potentially lead to performance issues, requiring careful optimization and data management strategies.
Code Examples
Here are a few code examples demonstrating the usage of react-map-gl
:
- Rendering a Basic Map:
import React from 'react';
import ReactMapGL from 'react-map-gl';
const App = () => {
const [viewport, setViewport] = React.useState({
latitude: 37.7577,
longitude: -122.4376,
zoom: 8,
width: '100vw',
height: '100vh'
});
return (
<ReactMapGL
{...viewport}
onViewportChange={setViewport}
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
>
</ReactMapGL>
);
};
export default App;
- Adding Markers to the Map:
import React from 'react';
import ReactMapGL, { Marker } from 'react-map-gl';
const App = () => {
const [viewport, setViewport] = React.useState({
latitude: 37.7577,
longitude: -122.4376,
zoom: 8,
width: '100vw',
height: '100vh'
});
const markers = [
{ latitude: 37.7577, longitude: -122.4376, label: 'San Francisco' },
{ latitude: 40.7128, longitude: -74.0060, label: 'New York' }
];
return (
<ReactMapGL
{...viewport}
onViewportChange={setViewport}
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
>
{markers.map((marker, index) => (
<Marker
key={index}
latitude={marker.latitude}
longitude={marker.longitude}
>
<div>{marker.label}</div>
</Marker>
))}
</ReactMapGL>
);
};
export default App;
- Handling User Interactions:
import React from 'react';
import ReactMapGL, { Popup } from 'react-map-gl';
const App = () => {
const [viewport, setViewport] = React.useState({
latitude: 37.7577,
longitude: -122.4376,
zoom
Competitor Comparisons
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
Pros of mapbox-gl-js
- Direct access to low-level WebGL rendering for advanced customization
- Broader ecosystem and extensive documentation
- More flexible for non-React applications
Cons of mapbox-gl-js
- Steeper learning curve for beginners
- Requires more boilerplate code for basic map setup
- Less seamless integration with React components
Code Comparison
mapbox-gl-js:
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'YOUR_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11'
});
react-map-gl:
import Map from 'react-map-gl';
function MyMap() {
return (
<Map
mapboxAccessToken="YOUR_ACCESS_TOKEN"
initialViewState={{
longitude: -100,
latitude: 40,
zoom: 3.5
}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v11"
/>
);
}
The code comparison shows that react-map-gl provides a more React-friendly approach with declarative syntax and easier integration of React components. mapbox-gl-js offers a more imperative style, giving developers finer control over map initialization and manipulation.
WebGL2 powered visualization framework
Pros of deck.gl
- More powerful and flexible for advanced data visualizations
- Supports a wider range of 2D and 3D layers
- Better performance for large datasets
Cons of deck.gl
- Steeper learning curve
- More complex setup and configuration
- Potentially overkill for simple mapping needs
Code Comparison
react-map-gl:
import Map from 'react-map-gl';
function MyMap() {
return <Map
initialViewState={{
longitude: -122.4,
latitude: 37.8,
zoom: 14
}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v9"
/>;
}
deck.gl:
import DeckGL from '@deck.gl/react';
import {ScatterplotLayer} from '@deck.gl/layers';
function MyDeckGLMap() {
const layers = [
new ScatterplotLayer({
data: [
{position: [-122.4, 37.8], color: [255, 0, 0], radius: 100}
],
getPosition: d => d.position,
getColor: d => d.color,
getRadius: d => d.radius
})
];
return <DeckGL
initialViewState={{longitude: -122.4, latitude: 37.8, zoom: 14}}
layers={layers}
/>;
}
Both libraries offer React components for mapping, but deck.gl provides more advanced visualization capabilities at the cost of increased complexity. react-map-gl is better suited for simpler mapping needs, while deck.gl excels in creating complex, data-heavy visualizations.
π JavaScript library for mobile-friendly interactive maps πΊπ¦
Pros of Leaflet
- Lightweight and fast, with a smaller footprint than React Map GL
- More extensive plugin ecosystem for additional functionality
- Easier to use for simple mapping applications without React
Cons of Leaflet
- Less seamless integration with React applications
- Limited support for advanced 3D visualizations and data-driven styling
- Performance may degrade with large datasets compared to React Map GL
Code Comparison
Leaflet:
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 Map GL:
import Map from 'react-map-gl';
function MyMap() {
return <Map
initialViewState={{longitude: -122.4, latitude: 37.8, zoom: 14}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v9"
/>;
}
Both libraries offer powerful mapping capabilities, but React Map GL is better suited for React-based applications and complex visualizations, while Leaflet excels in simplicity and broad compatibility across different web development environments.
OpenLayers
Pros of OpenLayers
- More comprehensive and feature-rich mapping library
- Supports a wider range of data formats and projections
- Better suited for complex GIS applications and advanced mapping needs
Cons of OpenLayers
- Steeper learning curve due to its extensive API and features
- Larger file size, which may impact load times for web applications
- Less seamless integration with React applications compared to react-map-gl
Code Comparison
OpenLayers:
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
const map = new Map({
target: 'map',
layers: [new TileLayer({ source: new OSM() })],
view: new View({ center: [0, 0], zoom: 2 })
});
react-map-gl:
import Map from 'react-map-gl';
function MyMap() {
return (
<Map
initialViewState={{ longitude: 0, latitude: 0, zoom: 2 }}
style={{ width: '100%', height: '400px' }}
mapStyle="mapbox://styles/mapbox/streets-v11"
/>
);
}
The code examples demonstrate the different approaches to creating a basic map. OpenLayers requires more setup and configuration, while react-map-gl offers a more React-friendly, declarative syntax.
MapLibre GL JS - Interactive vector tile maps in the browser
Pros of maplibre-gl-js
- Full-featured mapping library with extensive customization options
- Not tied to any specific framework, can be used in various environments
- Active community and frequent updates
Cons of maplibre-gl-js
- Steeper learning curve for React developers
- Requires more boilerplate code for React integration
- Less React-specific optimizations and features
Code Comparison
maplibre-gl-js:
import maplibregl from 'maplibre-gl';
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [-74.5, 40],
zoom: 9
});
react-map-gl:
import Map from 'react-map-gl';
function MyMap() {
return (
<Map
mapStyle="mapbox://styles/mapbox/streets-v9"
initialViewState={{
longitude: -74.5,
latitude: 40,
zoom: 9
}}
/>
);
}
react-map-gl is a React wrapper for mapbox-gl-js, providing a more React-friendly API and components. It offers easier integration with React applications and better performance optimizations for React. However, it may have limitations in customization compared to the more flexible maplibre-gl-js.
maplibre-gl-js is a fork of mapbox-gl-js, offering similar functionality without Mapbox-specific dependencies. It provides more control and customization options but requires more setup and configuration, especially in React environments.
Vector and raster maps with GL styles. Server side rendering by MapLibre GL Native. Map tile server for MapLibre GL JS, Android, iOS, Leaflet, OpenLayers, GIS via WMTS, etc.
Pros of tileserver-gl
- Self-hosted solution for serving vector tiles
- Supports multiple data sources (MBTiles, GeoJSON, PostGIS)
- Customizable styling using Mapbox GL styles
Cons of tileserver-gl
- Requires more setup and infrastructure management
- Limited built-in React integration compared to react-map-gl
- May have higher resource requirements for large datasets
Code Comparison
react-map-gl:
import Map from 'react-map-gl';
function MyMap() {
return <Map
initialViewState={{
longitude: -122.4,
latitude: 37.8,
zoom: 14
}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v9"
/>;
}
tileserver-gl (server-side configuration):
const tileserver = require('tileserver-gl');
const options = {
paths: {
root: __dirname,
styles: 'styles',
mbtiles: 'data'
}
};
tileserver.startServer(options);
Summary
react-map-gl is a React-specific library for integrating Mapbox GL into web applications, offering a more streamlined development experience for React developers. tileserver-gl, on the other hand, is a standalone server for hosting and serving vector tiles, providing more control over data sources and styling but requiring additional setup and management.
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-map-gl | Docs
react-map-gl
is a suite of React components designed to provide a React API for mapbox-gl or maplibre-gl. More information in the online documentation.
See our Design Philosophy.
Installation
Using react-map-gl
requires react >= 16.3
.
npm install --save react-map-gl mapbox-gl
Example
import * as React from 'react';
import Map from 'react-map-gl';
function App() {
return <Map
mapLib={import('mapbox-gl')}
initialViewState={{
longitude: -100,
latitude: 40,
zoom: 3.5
}}
style={{width: 600, height: 400}}
mapStyle="mapbox://styles/mapbox/streets-v9"
/>;
}
Using Mapbox Tokens
Starting with v2.0, mapbox-gl requires a Mapbox token for any usage, with or without the Mapbox data service. See about Mapbox tokens for your options.
To show maps from a service such as Mapbox you will need to register on their website in order to retrieve an access token required by the map component, which will be used to identify you and start serving up map tiles. The service will be free until a certain level of traffic is exceeded.
There are several ways to provide a token to your app, as showcased in some of the example folders:
- Provide a
mapboxAccessToken
prop to the map component - Set the
MapboxAccessToken
environment variable (or setREACT_APP_MAPBOX_ACCESS_TOKEN
if you are using Create React App) - Provide it in the URL, e.g
?access_token=TOKEN
Contribute
See contribution guide.
Attributions
react-map-gl is part of vis.gl, an Urban Computing Foundation project.
Development is also supported by
Top Related Projects
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
WebGL2 powered visualization framework
π JavaScript library for mobile-friendly interactive maps πΊπ¦
OpenLayers
MapLibre GL JS - Interactive vector tile maps in the browser
Vector and raster maps with GL styles. Server side rendering by MapLibre GL Native. Map tile server for MapLibre GL JS, Android, iOS, Leaflet, OpenLayers, GIS via WMTS, etc.
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