Convert Figma logo to code with AI

alex3165 logoreact-mapbox-gl

A React binding of mapbox-gl-js

1,910
535
1,910
265

Top Related Projects

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL

MapLibre GL JS - Interactive vector tile maps in the browser

Quick Overview

React-mapbox-gl is a React wrapper for Mapbox GL JS, providing a set of React components for building interactive maps. It allows developers to easily integrate Mapbox's powerful mapping capabilities into React applications, offering a more declarative approach to map creation and management.

Pros

  • Seamless integration with React applications
  • Declarative API for easier map manipulation
  • Supports custom layers and controls
  • Automatic handling of map lifecycle and performance optimizations

Cons

  • Requires a Mapbox access token, which may have usage limits
  • Learning curve for developers unfamiliar with Mapbox GL JS
  • Limited documentation compared to the official Mapbox GL JS library
  • May lag behind in supporting the latest Mapbox GL JS features

Code Examples

  1. Creating a basic map:
import ReactMapboxGl from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: 'your-mapbox-access-token'
});

const App = () => (
  <Map
    style="mapbox://styles/mapbox/streets-v11"
    containerStyle={{
      height: '100vh',
      width: '100vw'
    }}
  />
);
  1. Adding a marker to the map:
import ReactMapboxGl, { Marker } from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: 'your-mapbox-access-token'
});

const App = () => (
  <Map
    style="mapbox://styles/mapbox/streets-v11"
    center={[-74.5, 40]}
    zoom={[9]}
  >
    <Marker coordinates={[-74.5, 40]}>
      <img src="marker.png" />
    </Marker>
  </Map>
);
  1. Adding a GeoJSON layer:
import ReactMapboxGl, { GeoJSONLayer } from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: 'your-mapbox-access-token'
});

const geojson = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [-77.0323, 38.9131]
      },
      properties: {
        title: 'Mapbox DC',
        icon: 'monument'
      }
    }
  ]
};

const App = () => (
  <Map style="mapbox://styles/mapbox/streets-v11">
    <GeoJSONLayer
      data={geojson}
      symbolLayout={{
        "icon-image": "monument-15",
        "text-field": "{title}",
        "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
        "text-offset": [0, 0.6],
        "text-anchor": "top"
      }}
    />
  </Map>
);

Getting Started

  1. Install the package:

    npm install react-mapbox-gl mapbox-gl
    
  2. Import and use in your React application:

    import ReactMapboxGl from 'react-mapbox-gl';
    
    const Map = ReactMapboxGl({
      accessToken: 'your-mapbox-access-token'
    });
    
    function App() {
      return (
        <Map
          style="mapbox://styles/mapbox/streets-v11"
          containerStyle={{
            height: '400px',
            width: '100%'
          }}
        />
      );
    }
    
  3. Make sure to include the Mapbox GL CSS in your project:

    <link href='https://api.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.css' rel='stylesheet' />
    

Competitor Comparisons

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL

Pros of mapbox-gl-js

  • More comprehensive and feature-rich, offering direct access to Mapbox GL JS functionality
  • Better performance for complex map interactions and large datasets
  • Wider community support and more frequent updates

Cons of mapbox-gl-js

  • Steeper learning curve, especially for React developers
  • Requires more boilerplate code for integration with React applications
  • Less React-specific optimizations and component-based structure

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-mapbox-gl:

import ReactMapboxGl from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: 'YOUR_ACCESS_TOKEN'
});

<Map
  style="mapbox://styles/mapbox/streets-v11"
  containerStyle={{height: '100vh', width: '100vw'}}
/>

The mapbox-gl-js library provides a more direct approach to map creation, while react-mapbox-gl offers a more React-friendly component-based implementation. The choice between the two depends on the specific needs of the project, with mapbox-gl-js offering more flexibility and power, and react-mapbox-gl providing easier integration with React applications.

MapLibre GL JS - Interactive vector tile maps in the browser

Pros of maplibre-gl-js

  • Fully open-source and free to use without restrictions
  • Supports a wider range of map styles and data sources
  • More active development and community support

Cons of maplibre-gl-js

  • Steeper learning curve for React developers
  • Requires more setup and configuration for React integration
  • Less React-specific documentation and examples

Code Comparison

react-mapbox-gl:

import ReactMapboxGl from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: 'your-mapbox-access-token'
});

<Map
  style="mapbox://styles/mapbox/streets-v9"
  containerStyle={{height: '100vh', width: '100vw'}}
/>

maplibre-gl-js:

import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';

useEffect(() => {
  const map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [0, 0],
    zoom: 1
  });
}, []);

The react-mapbox-gl library provides a more React-friendly API, while maplibre-gl-js offers more flexibility and control over map initialization and management. The choice between the two depends on the specific needs of your project and your familiarity with React and mapping libraries.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Logo

React-mapbox-gl | Documentation | Demos

Build Status npm version npm downloads TypeScript

React wrapper for mapbox-gl-js.



London cycle example gif

Components

Proxy components (proxy between React and Mapbox API)

  • ReactMapboxGL
  • Layer & Feature
    • property symbol displays a mapbox symbol.
    • property line displays a lineString.
    • property fill displays a polygon.
    • property circle displays a mapbox circle.
    • property raster displays a mapbox raster tiles.
    • property fill-extrusion displays a layer with extruded buildings.
    • property background displays a mapbox background layer.
    • property heatmap displays a mapbox heatmap layer.
  • Source
  • GeoJSONLayer

DOM components (normal React components)

  • ZoomControl
  • ScaleControl
  • RotationControl
  • Marker (Projected component)
  • Popup (Projected component)
  • Cluster

Getting Started

npm install react-mapbox-gl mapbox-gl --save

Example:

Adding the css in your index.html:

<html>
  <head>
    ...
    <link
      href="https://api.mapbox.com/mapbox-gl-js/v1.10.1/mapbox-gl.css"
      rel="stylesheet"
    />
  </head>
</html>
// ES6
import ReactMapboxGl, { Layer, Feature } from 'react-mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

// ES5
var ReactMapboxGl = require('react-mapbox-gl');
var Layer = ReactMapboxGl.Layer;
var Feature = ReactMapboxGl.Feature;
require('mapbox-gl/dist/mapbox-gl.css');

const Map = ReactMapboxGl({
  accessToken:
    'pk.eyJ1IjoiZmFicmljOCIsImEiOiJjaWc5aTV1ZzUwMDJwdzJrb2w0dXRmc2d0In0.p6GGlfyV-WksaDV_KdN27A'
});

// in render()
<Map
  style="mapbox://styles/mapbox/streets-v9"
  containerStyle={{
    height: '100vh',
    width: '100vw'
  }}
>
  <Layer type="symbol" id="marker" layout={{ 'icon-image': 'marker-15' }}>
    <Feature coordinates={[-0.481747846041145, 51.3233379650232]} />
  </Layer>
</Map>;

Why are zoom, bearing and pitch Arrays ?

If those properties changed at the mapbox-gl-js level and you don't update the value kept in your state, it will be unsynced with the current viewport. At some point you might want to update the viewport value (zoom, pitch or bearing) with the ones in your state but using value equality is not enough. Taking zoom as example, you will still have the unsynced zoom value therefore we can't tell if you want to update the prop or not. In order to explicitly update the current viewport values you can instead break the references of those props and reliably update the current viewport with the one you have in your state to be synced again.

Current version documentation

Version 3.0 documentation

Version 2.0 documentation

Contributions

Please try to reproduce your problem with the boilerplate before posting an issue.

mapbox-gl-draw compatibility

Try react-mapbox-gl-draw

Looking for an Angular alternative?

Try ngx-mapbox-gl

NPM DownloadsLast 30 Days