2.0.3 • Published 6 years ago

@commodityvectors/react-mapbox-gl v2.0.3

Weekly downloads
66
License
MIT
Repository
github
Last release
6 years ago

React Mapbox GL Build Status npm npm npm

A simple and customizable MapboxGL wrapper for react

Edit React Mapbox GL

Getting Started

npm install @commodityvectors/react-mapbox-gl --save

Make sure you have all peers dependencies required installed on your project.

React VersionReact Mapbox GL Version
16.3.x2.x.x
16.2.x1.x.x

Example:

import React from 'react';
import { Map } from '@commodityvectors/react-mapbox-gl';

export default class MyMap extends React.Component {
    render() {
        return (
            <Map {...this.props} accessToken={"YOUR_ACCESS_TOKEN"} mapStyle={'mapbox://styles/mapbox/streets-v9'}>
                {this.props.children}            
            </Map>
        );
    }
}

Creating Custom Map Components

It is pretty simple to write your own map components. This library takes advantage of the new React 16.3 context API so you can enrich you component with Map.component() method like below. This helper will automatically add a MapContext.Provider and inject the map as a prop.

Example:

import React from 'react';
import mapboxgl from 'mapbox-gl';

import { Map } from '@commodityvectors/react-mapbox-gl';


class FullscreenControl extends React.Component {
    componentDidMount() {
        const { map } = this.props;
        this.control = new mapboxgl.FullscreenControl();
        map.addControl(this.control);
    }
    
    componentWillUnmount() {
        const { map } = this.props;
        if(!map.isRemoving()) map.removeControl(this.control);
    }

    render() {
        return null; // As this component won't change through render methods
    }
}

export default Map.component(FullscreenControl);

Extending existing components

In order to extend an existing component, you have to propagate the props (or at least the map prop) to the child component as the example below shows

import React from 'react';
import { CustomControl, Map } from '@commodityvectors/react-mapbox-gl';


class Control extends React.PureComponent {
    state = {
        position: 'top-right'
    };

    recenter = () => {
        this.props.map.easeTo({
            center: [0, 0],
            zoom: 1
        });
    };

    render() {
        const positions = {
            'top-right': 'bottom-left',
            'bottom-left': 'top-right'
        };


        return (
            <CustomControl position={this.state.position} map={this.props.map}> {/* {...this.props} pass the map instance to the component below */}
                <button style={{
                    'width': '120px',
                    'padding': '5px'
                }} onClick={() => this.setState({position: positions[this.state.position]})}>Change Position</button>

                <button style={{
                    'width': '120px',
                    'padding': '5px'
                }} onClick={this.recenter}>Recenter</button>
            </CustomControl>
        );
    }
}

export default Map.component(Control);

Components

Map

Property NameDescriptionTypeRequired
accessTokenMapbox access tokenstringYES
mapStyleThe map 'style' propertyobject/stringYES
bearingThe map 'bearing' propertynumber
pitchThe map 'pitch' propertynumber
zoomThe map 'zoom' propertynumber
centerThe map 'center' propertyobject/Array
optionsEvery other map propertyobject
onViewPortChangeCalled when bearing, pitch, center or zoom changesfunction
mapNodeStyleThe map parent component styleobject

Layer

Property NameDescriptionTypeRequired
idThe layer idstringYES
typeThe layer typestringYES
visibleShortcut to set paint property "visibility"boolean
beforeThe ID of an existing layer to add thi one beforestring
optionsEvery other layer properties, you can use cameCaseobject

Source

Property NameDescriptionTypeRequired
idThe layer idstringYES
typeLayer type (as found on mapbox documentation)stringYES
optionsEvery other source propertiesobjectYES

PopUp

Property NameDescriptionTypeRequired
positionThis popup coordinatesArray/objectYES
onCloseCalled when popup is closedfunction
optionsEvery other popup propertiesobject

Marker

Property NameDescriptionTypeRequired
positionThis marker coordinatesArray/objectYES
onClickCalled when marker is clickedfunction
dataData to be sent in click eventany

AttributionControl

Property NameDescriptionTypeRequired
positionThis control positionstring
optionsEvery other propertiesobject

NavigationControl

Property NameDescriptionTypeRequired
positionThis control positionstring
optionsEvery other propertiesobject

ScaleControl

Property NameDescriptionTypeRequired
positionThis control positionstring
optionsEvery other propertiesobject

GeolocateControl

Property NameDescriptionTypeRequired
positionThis control positionstring
optionsEvery other propertiesobject

FullscreenControl

Property NameDescriptionTypeRequired
positionThis control positionstring

CustomControl

Property NameDescriptionTypeRequired
positionThis control position (top-right, bottom-left,...)string
2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-rc2

6 years ago

1.0.0-rc1

6 years ago

0.0.1

6 years ago