Mapbox GL app with react and mapbox gl does not work

Viewed 142

I followed the tutorial on Mapbox using react and Mapbox GL and there are no problems related to the code but when I start the application on the browser(chrome) it shows a blank page with the coordinates. it seems that the application itself works but it does not shows the Map.

this is my code:

html

<!DOCTYPE html>
<html lang='en'>
<head>
<title>Mapbox GL JS and React</title>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css' rel='stylesheet' />
<link href='site.css' rel='stylesheet'/>
</head>
<body>
<div id='app'></div>
</body>
</html>

css:

.sidebarStyle {
display: inline-block;
position: absolute;
top: 0;
left: 0;
margin: 12px;
background-color: #404040;
color: #ffffff;
z-index: 1 !important;
padding: 6px;
font-weight: bold;
}
 
.mapContainer {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}

js:

import React from 'react';
import ReactDOM from 'react-dom';
import mapboxgl from 'mapbox-gl';
 
mapboxgl.accessToken = 'MAPBOX_ACCESS_TOKEN';
 
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: 5,
lat: 34,
zoom: 2
};
}
 
componentDidMount() {
const map = new mapboxgl.Map({
container: this.mapContainer,
style: 'mapbox://styles/mapbox/streets-v11',
center: [this.state.lng, this.state.lat],
zoom: this.state.zoom
});
 
map.on('move', () => {
this.setState({
lng: map.getCenter().lng.toFixed(4),
lat: map.getCenter().lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
 
render() {
return (
<div>
<div className='sidebarStyle'>
<div>Longitude: {this.state.lng} | Latitude: {this.state.lat} | Zoom: {this.state.zoom}</div>
</div>
<div ref={el => this.mapContainer = el} className='mapContainer' />
</div>
)
}
}
 
ReactDOM.render(<Application />, document.getElementById('app'));
0 Answers
Related