react-map-gl 0,2,4,1,4,0,125,3 editorconfig eslint travis-ci npm

React friendly API wrapper around MapboxGL JS

version build downloads

react-map-gl | Docs

React friendly API wrapper around MapboxGL JS

screen

This project is new and the API may change.

npm install --save react-map-gl

Using with Browserify, Webpack etc

  • browserify - react-map-gl is extensively tested with browserify and works without configuration.

  • webpack 1 - look at the exhibit-webpack folder, demonstrating a working demo using webpack.

  • webpack 2 - The dev branch in this repo is based on webpack 2, look at the webpack config file in the main example.

In general, for non-browserify based environments, make sure you have read the instructions on the mapbox-gl-js README.

Example

import MapGL from 'react-map-gl';

<MapGL
  width={400}
  height={400}
  latitude={37.7577}
  longitude={-122.4376}
  zoom={8}
  onChangeViewport={viewport => {
    const {latitude, longitude, zoom} = viewport;
    // Optionally call `setState` and use the state to update the map.
  }}
/>

Overlays

react-map-gl provides an overlay API so you can use the built-in visualization overlays, or create your own.

import {ScatterplotOverlay} from 'react-map-gl/overlays';

<MapGL {...viewport}>
  <ScatterplotOverlay
    {...viewport}
    locations={locations}
    dotRadius={4}
    globalOpacity={1}
    compositeOperation="screen" />
</MapGL>

Built-in overlays are: ChoroplethOverlay, ScatterplotOverlay, DraggablePointsOverlay, SVGOverlay and CanvasOverlay. They are imported using

import {SVGOverlay, ...} from 'react-map-gl/overlays';

Remarks:

  • These overlays are currently not tested with perspective mode, although they should in theory be compatible with perspective enabled viewports in viewport-mercator-project
  • In v1, overlays were exported directly from 'react-map-gl'.

deck.gl Layers

deck.gl is a companion module to react-map-gl that provide a number of classic data visualization overlays (scatterplots, choropleths etc) implemented in WebGL. These overlays are suitable for large and/or dynamic data sets, or for use in perspective mode applications.

Third-party Overlays

Third party overlays can also be created. For example, the heatmap-overlay uses webgl-heatmap to create geographic heatmaps.

import HeatmapOverlay from 'react-map-gl-heatmap-overlay';
import cities from 'example-cities';

<MapGL {...viewport}>
  <HeatmapOverlay locations={cities} {...viewport} />
</MapGL>

Want to create and share your own overlay? Fork the react-map-gl-example-overlay project to get started.

Perspective Mode

Perspective mode is exposed using the pitch and bearing props (both default to 0), which will show the map "tilted" pitch degrees (overhead being 0 degrees), looking towards bearing (0 degrees is north).

In addition, the perspectiveEnabled prop (default: false) will activate mouse handlers that allow the user to change pitch and bearing using the mouse while holding down any function key {command, shift, ctrl, alt}.

If perspectiveEnabled is not set to true then the user will not be able to change the pitch and bearing, which means that the default props will show an overhead map and only enable standard pan and zoom mouse actions on that map.

Considerations:

  • Mapbox-gl-js limits the pitch to 60 degrees.
  • When using pitch, several additional fields are passed in the onViewportChange callback, make sure to pass all received props back to the component.
  • Not all overlays are compatible with perspective mode. For a set of overlays that do work with perspective mode, look at deck.gl.

Transitions

react-map-gl does not expose the transition API for mapbox-gl-js since it is designed to be a stateless component.

Instead it is recommended to use a separate module like react-motion to animate properties.

<Motion style={{
  latitude: spring(viewport.latitude, { stiffness: 170, damping: 26, precision: 0.000001 }),
  longitude: spring(viewport.longitude, { stiffness: 170, damping: 26, precision: 0.000001 })
}}>
  {({ latitude, longitude }) => <MapGL
    {...viewport}
    latitude={latitude}
    longitude={longitude}
    mapStyle={mapboxStyle}
  />}
</Motion>

ImmutableJS

The mapStyle property of the MapGL as well as several of the built in overlay properties must be provided as ImmutableJS objects. This allows the library to be fast since computing changes to props only involves checking if the immutable objects are the same instance.

Redux

If you're using redux, it is relatively simple to hook this component up to store state in the redux state tree. The simplest way is to take all properties passed to the onChangeViewport function property and add them directly into the store. This state can then be passed back to react-map-gl without any transformation. You can use the package redux-map-gl to save writing this code yourself.

Development

Install project dependencies and check that the tests run

npm install
npm test

Note on yarn: yarn does not appear to be compatible with mapbox-gl v0.31, in terms of installing correct versions of dependencies needed to run test scripts in the folder. While this is likely to change, for now it is recommended it is recommended to use npm install in the root folder of react-map-gl. This restriction only applies when installing in the root folder; yarn is still supported and recommended when installing the examples in the examples folder.

Then start the main example in examples/main by running the shortcut

npm start

This should open a new tab in your browser with the examples. To make the maps load, either:

  • add ?access_token=TOKEN to the URL where TOKEN is a valid Mapbox access token, or
  • set the MapboxAccessToken environment variable before running npm start

Testing

  • Unit Tests - it is expected that any feature is accompanied by standard unit tests in test folder.

  • Unit Tests in Node: Note that This component uses WebGL, but it still runs under Node.js (using headless-gl and jsdom). This is how the standard npm test script runs.

  • Unit Tests in Browser - It is also important to run unit tests in the browser, via npm run test-browser.

  • Manual Testing - In addition, please test drive new and existing features by running npm start and manually testing the demos.

Contributing

Contributions are welcome. It can be helpful to check with maintainers before opening your PR, just open an issue and describe your proposal. Also, be aware that you will need to complete a short open source contribution form before your pull request can be accepted.

Related Repositories

awesome-react

awesome-react

A collection of awesome things regarding React ecosystem. ...

awesome-react-components

awesome-react-components

Catalog of React Components & Libraries ...

awesome-react-me

awesome-react-me

A collection of awesome things regarding React ecosystem for me. ...

react-mapbox-gl-js

react-mapbox-gl-js

Mapbox-gl-js react component test ...

awesome-react

awesome-react

A collection of awesome things regarding React ecosystem. ...


Top Contributors

vicapow ibgreen abmai Pessimistress philogb Willyham mattdesl wwwtyro tpyl cammanderson drewbo tsemerad vinkla chrisirhc AndyMoreland DenisCarriere jackiewung jwarning lafin lxe mdamien

Dependencies

package version
bowser ^1.2.0
hammerjs ^2.0.8
immutable *
mapbox-gl 0.38.0
prop-types ^15.5.7
viewport-mercator-project ^4.0.1
dev babel-cli ^6.22.2
babel-core ^6.22.1
babel-eslint ^6.0.0
babel-plugin-transform-es2015-modules-commonjs ^6.24.0
babel-preset-es2015 ^6.18
eslint ^3.0.0
eslint-config-uber-es2015 ^3.0.0
eslint-config-uber-jsx ^3.0.0
eslint-plugin-react ~6.7.0
flow-remove-types ^1.1.2
gl ^4.0.3
glob ^7.1.1
jsdom ~9.9.1
module-alias ^2.0.0
path-exists ^3.0.0
pre-commit ^1.2.2
react ^15.4.0
react-addons-test-utils ^15.4.0
react-dom ^15.4.0
reify ^0.4.4
remove-flow-types-loader ^1.0.0
sinon ^1.17.7
tap-browser-color ^0.1.2
tape ^4.5.1
tape-catch ^1.0.4
webpack ^2.4.0
webpack-dev-server ^2.4.0
webworkify-webpack-dropin ^1.1.9
peer react >=15.4.x

Releases

-   v1.6.1 zip tar
-   v1.6.0 zip tar
-   v1.4.2 zip tar
-   v1.4.1 zip tar
-   v1.4.0 zip tar
-   v1.3.0 zip tar
-   v1.2.0 zip tar
-   v1.0.0 zip tar
-   v0.6.7 zip tar
-   v0.6.6 zip tar
-   v0.6.5 zip tar
-   v0.6.4 zip tar
-   v0.6.3 zip tar
-   v0.6.2 zip tar
-   v0.6.1 zip tar
-   v0.6.0 zip tar
-   v0.5.1 zip tar
-   v0.5.0 zip tar
-   v0.4.0 zip tar
-   v0.3.0 zip tar
-   v0.2.4 zip tar
-   v0.2.3 zip tar
-   v0.2.2 zip tar
-   v0.2.0 zip tar