• Stars
    star
    416
  • Rank 104,068 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Component Library for Mapbox GL JS

Urbica React Mapbox GL JS

Node CI codecov npm npm npm bundle size (scoped)

React Component Library for Mapbox GL JS. Mapbox GL JS is a JavaScript library that renders interactive maps from vector tiles and Mapbox styles using WebGL. This project is intended to be as close as possible to the Mapbox GL JS API.

This project is heavily inspired by uber/react-map-gl.

Gallery

Installation

npm install --save mapbox-gl @urbica/react-map-gl

...or if you are using yarn:

yarn add mapbox-gl @urbica/react-map-gl

Optional Dependencies

If you want to use the LanguageControl:

npm install --save @mapbox/mapbox-gl-language

...or if you are using yarn:

yarn add @mapbox/mapbox-gl-language

Components

Component Description
MapGL Represents map on the page
MapContext React Context API for the map instance
Source Sources specify the geographic features to be rendered on the map
Layer Layers specify the Sources style
Filter Set filter to existing layer
CustomLayer Allow a user to render directly into the map's GL context
Image Adds an image to the map style
Popup React Component for Mapbox GL JS Popup
Marker React Component for Mapbox GL JS Marker
FeatureState Sets the state of a geographic feature rendered on the map
AttributionControl Represents the map's attribution information
LanguageControl Adds support for switching the language of the map style
FullscreenControl Contains a button for toggling the map in and out of fullscreen mode
GeolocateControl Geolocate the user and then track their current location on the map
NavigationControl Contains zoom buttons and a compass
ScaleControl Displays the ratio of a distance on the map to the corresponding distance on the ground
Cluster Cluster Markers with supercluster
Draw Support for drawing and editing features

Usage

To use any of Mapboxโ€™s tools, APIs, or SDKs, youโ€™ll need a Mapbox access token. Mapbox uses access tokens to associate requests to API resources with your account. You can find all your access tokens, create new ones, or delete existing ones on your API access tokens page.

See Documentation for more examples.

Static Map

By default, MapGL component renders in a static mode. That means that the user cannot interact with the map.

import React from 'react';
import MapGL from '@urbica/react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

<MapGL
  style={{ width: '100%', height: '400px' }}
  mapStyle='mapbox://styles/mapbox/light-v9'
  accessToken={MAPBOX_ACCESS_TOKEN}
  latitude={37.78}
  longitude={-122.41}
  zoom={11}
/>;

Interactive Map

In most cases, you will want the user to interact with the map. To do this, you need to provide onViewportChange handler, that will update map viewport state.

import React, { useState } from 'react';
import MapGL from '@urbica/react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

const [viewport, setViewport] = useState({
  latitude: 37.78,
  longitude: -122.41,
  zoom: 11
});

<MapGL
  style={{ width: '100%', height: '400px' }}
  mapStyle='mapbox://styles/mapbox/light-v9'
  accessToken={MAPBOX_ACCESS_TOKEN}
  latitude={viewport.latitude}
  longitude={viewport.longitude}
  zoom={viewport.zoom}
  onViewportChange={setViewport}
/>;

MapGL with Source and Layer

Sources specify the geographic features to be rendered on the map.

Layers specify the Sources styles. The type of layer is specified by the "type" property, and must be one of background, fill, line, symbol, raster, circle, fill-extrusion, heatmap, hillshade.

Except for layers of the background type, each layer needs to refer to a source. Layers take the data that they get from a source, optionally filter features, and then define how those features are styled.

import React from 'react';
import MapGL, { Source, Layer } from '@urbica/react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

<MapGL
  style={{ width: '100%', height: '400px' }}
  mapStyle='mapbox://styles/mapbox/light-v9'
  accessToken={MAPBOX_ACCESS_TOKEN}
>
  <Source id='contours' type='vector' url='mapbox://mapbox.mapbox-terrain-v2' />
  <Layer
    id='contours'
    type='line'
    source='contours'
    source-layer='contour'
    paint={{
      'line-color': '#877b59',
      'line-width': 1
    }}
  />
</MapGL>;

MapGL with GeoJSON Source

To draw a GeoJSON on a map, add Source with the type property set to geojson and data property set to a URL or inline GeoJSON.

import React, { useState } from 'react';
import MapGL, { Source, Layer } from '@urbica/react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

const [viewport, setViewport] = useState({
  latitude: 37.830348,
  longitude: -122.486052,
  zoom: 15
});

const data = {
  type: 'Feature',
  geometry: {
    type: 'LineString',
    coordinates: [
      [-122.48369693756104, 37.83381888486939],
      [-122.48348236083984, 37.83317489144141],
      [-122.48339653015138, 37.83270036637107],
      [-122.48356819152832, 37.832056363179625],
      [-122.48404026031496, 37.83114119107971],
      [-122.48404026031496, 37.83049717427869],
      [-122.48348236083984, 37.829920943955045],
      [-122.48356819152832, 37.82954808664175],
      [-122.48507022857666, 37.82944639795659],
      [-122.48610019683838, 37.82880236636284],
      [-122.48695850372314, 37.82931081282506],
      [-122.48700141906738, 37.83080223556934],
      [-122.48751640319824, 37.83168351665737],
      [-122.48803138732912, 37.832158048267786],
      [-122.48888969421387, 37.83297152392784],
      [-122.48987674713133, 37.83263257682617],
      [-122.49043464660643, 37.832937629287755],
      [-122.49125003814696, 37.832429207817725],
      [-122.49163627624512, 37.832564787218985],
      [-122.49223709106445, 37.83337825839438],
      [-122.49378204345702, 37.83368330777276]
    ]
  }
};

<MapGL
  style={{ width: '100%', height: '400px' }}
  mapStyle='mapbox://styles/mapbox/light-v9'
  accessToken={MAPBOX_ACCESS_TOKEN}
  onViewportChange={setViewport}
  {...viewport}
>
  <Source id='route' type='geojson' data={data} />
  <Layer
    id='route'
    type='line'
    source='route'
    layout={{
      'line-join': 'round',
      'line-cap': 'round'
    }}
    paint={{
      'line-color': '#888',
      'line-width': 8
    }}
  />
</MapGL>;

Custom Layers support

Custom layers allow a user to render directly into the map's GL context using the map's camera.

Here is an Uber deck.gl usage example.

import React from 'react';
import MapGL, { CustomLayer } from '@urbica/react-map-gl';
import { MapboxLayer } from '@deck.gl/mapbox';
import { ScatterplotLayer } from '@deck.gl/layers';
import 'mapbox-gl/dist/mapbox-gl.css';

const myDeckLayer = new MapboxLayer({
  id: 'my-scatterplot',
  type: ScatterplotLayer,
  data: [{ position: [-74.5, 40], size: 1000 }],
  getPosition: (d) => d.position,
  getRadius: (d) => d.size,
  getColor: [255, 0, 0]
});

<MapGL
  style={{ width: '100%', height: '400px' }}
  mapStyle='mapbox://styles/mapbox/light-v9'
  accessToken={MAPBOX_ACCESS_TOKEN}
  latitude={40}
  longitude={-74.5}
  zoom={9}
>
  <CustomLayer layer={myDeckLayer} />
</MapGL>;

Documentation

Check out documentation website.

Changelog

Check out CHANGELOG.md and releases page.

License

This project is licensed under the terms of the MIT license.

Contributing

Clone and install dependencies

git clone https://github.com/urbica/react-map-gl.git
cd react-map-gl
npm install

Start react-styleguidist server

MAPBOX_ACCESS_TOKEN=<TOKEN> npm start

where <TOKEN> is a valid Mapbox access token.

Run tests with

npm test

Team

Stepan Kuzmin Artem Boyur Andrey Bakhvalov
Stepan Kuzmin Artem Boyur Andrey Bakhvalov

More Repositories

1

galton

Lightweight Node.js isochrone map server
JavaScript
268
star
2

tessella

Lightweight Node.js Mapbox Vector Tiles server
JavaScript
83
star
3

react-map-gl-draw

React Component for Mapbox GL Draw
JavaScript
50
star
4

gis-notebook

Jupyter Notebook Python GIS Stack
44
star
5

pg-migrate

PostgreSQL migration tool
JavaScript
42
star
6

bikes

Bike share data visualisation
JavaScript
37
star
7

react-map-gl-cluster

Urbica React Cluster Component for Mapbox GL JS
JavaScript
26
star
8

noisemap

The project for MUF-Labs hackathon
HTML
19
star
9

areal

An iOS augmented reality app with a map of Saint Petersburg and itโ€™s 3D landmark models
C#
17
star
10

ui-kit

Urbica UI Kit ๐Ÿ‹
JavaScript
16
star
11

osm-extractor

Extracts data from OpenStreetMap
JavaScript
15
star
12

walkstreets

Walking Streets project
HTML
15
star
13

docker-otp

OpenTripPlanner Docker image
Dockerfile
13
star
14

citibike-nyc

Mapbox GL JS feature state usage example
HTML
11
star
15

velodata

Bike share statistics 2015 in Moscow
10
star
16

node-martin

Mapbox Vector Tiles Server ๐Ÿฆ
JavaScript
7
star
17

datamos-geojson

Small tool to download geoJSON files from data.mos.ru
JavaScript
6
star
18

charts

Urbica React D3 Components
JavaScript
5
star
19

osrm-bindings

The Open Source Routing Machine bindings wrapper for NodeJS
JavaScript
4
star
20

urbanlayers

Small geospatial research
JavaScript
3
star
21

citibike

Citibike NYC Rebalanced
JavaScript
3
star
22

docker-postgis

PostGIS Docker image
Shell
3
star
23

martin-landing

JavaScript
2
star
24

hurricane-map

JavaScript
2
star
25

taxi-trips

HTML
2
star
26

map-tab-gl

JavaScript
2
star
27

workshop

HTML
2
star
28

gulag-exposition

Gulag map exposition UI
JavaScript
2
star
29

map-compare-tool

Small tool to compare different maps
JavaScript
1
star
30

eslint-config-urbica

Urbica ESLint config
JavaScript
1
star
31

homebrew-tap

macOS Homebrew formulas to install Urbica open source software ๐Ÿบ
Ruby
1
star
32

gulag-front

Gulag map user interface
JavaScript
1
star
33

gulag-ui-kit

A list of common components for Gulag web and exposure versions
JavaScript
1
star
34

humans

Humans prototype
JavaScript
1
star
35

gulag-admin

Gulag map administrative UI
JavaScript
1
star
36

albatros

Rust
1
star
37

stuurman

Wicked router
Python
1
star
38

walksreets-ios

Walking Streets iOS mobile app
Swift
1
star
39

gulag-api

Gulag map API
JavaScript
1
star