You can get the mapbox-gl map object once the component mounts, then you can work with it directly. Try something like this:
class MapPage extends Component {
constructor(props) {
super(props);
this.state = {
viewport: {
latitude: 38.63738602787579,
longitude: -121.23576311149986,
zoom: 6.8,
bearing: 0,
pitch: 0,
dragPan: true,
width: 600,
height: 600
}
};
}
componentDidMount(){
const map = this.reactMap.getMap();
map.on('load', () => {
//add the GeoJSON layer here
map.addLayer({...})
})
}
render() {
const { viewport } = this.state;
return (
<ReactMapGL
ref={(reactMap) => this.reactMap = reactMap} />
{...viewport}
mapboxApiAccessToken={MAPBOX_TOKEN}
onViewportChange={newViewport => {
this.setState({ viewport: newViewport });
}}
/>
);
}
}
React Refs: https://reactjs.org/docs/refs-and-the-dom.html
GetMap(): https://uber.github.io/react-map-gl/#/Documentation/api-reference/static-map?section=methods
Answer from Kristofor Carle on Stack Overflow
» npm install react-map-gl-draw
I am trying to use the react-map-gl geolocation example but .geojson data not willing to load - Stack Overflow
How to edit and interact with existing GeoJSON data with react-map-gl
oop - ReactMapGL add points from geoJSON - Stack Overflow
reactjs - How to use a local GeoJSON file with Mapbox in React - Stack Overflow
You can get the mapbox-gl map object once the component mounts, then you can work with it directly. Try something like this:
class MapPage extends Component {
constructor(props) {
super(props);
this.state = {
viewport: {
latitude: 38.63738602787579,
longitude: -121.23576311149986,
zoom: 6.8,
bearing: 0,
pitch: 0,
dragPan: true,
width: 600,
height: 600
}
};
}
componentDidMount(){
const map = this.reactMap.getMap();
map.on('load', () => {
//add the GeoJSON layer here
map.addLayer({...})
})
}
render() {
const { viewport } = this.state;
return (
<ReactMapGL
ref={(reactMap) => this.reactMap = reactMap} />
{...viewport}
mapboxApiAccessToken={MAPBOX_TOKEN}
onViewportChange={newViewport => {
this.setState({ viewport: newViewport });
}}
/>
);
}
}
React Refs: https://reactjs.org/docs/refs-and-the-dom.html
GetMap(): https://uber.github.io/react-map-gl/#/Documentation/api-reference/static-map?section=methods
Applicable for those who are using react-map-gl Version 5.0 onwards
As of October 2019, react-map-gl supports Layer and Source components, which is meant to allow developers to render Mapbox layers on the Mapbox canvas without the need to call getMap() to expose the underlying native Mapbox API.
You may refer to the original Mapbox Source and Layer documentations for the full specifications for the valid values for the Layer and Source props.
This is how the Source and Layer components can be used together with the code you have provided to generatea GeoJSON line on your map.
class MapPage extends Component {
constructor(props) {
super(props);
this.state = {
viewport: {
latitude: 38.63738602787579,
longitude: -121.23576311149986,
zoom: 6.8,
bearing: 0,
pitch: 0,
dragPan: true,
width: 600,
height: 600
}
};
}
render() {
const { viewport } = this.state;
return (
<ReactMapGL
{...viewport}
mapboxApiAccessToken={MAPBOX_TOKEN}
onViewportChange={newViewport => {
this.setState({ viewport: newViewport });
}}
>
<Source id='polylineLayer' type='geojson' data={polylineGeoJSON}>
<Layer
id='lineLayer'
type='line'
source='my-data'
layout={{
'line-join': 'round',
'line-cap': 'round',
}}
paint={{
'line-color': 'rgba(3, 170, 238, 0.5)',
'line-width': 5,
}}
/>
</Source>
</ReactMapGL>
);
}
}