Skip to content

Instantly share code, notes, and snippets.

@anujsinghwd
Last active August 22, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anujsinghwd/409d0c0fca67806e7bd9024b6d0d63f7 to your computer and use it in GitHub Desktop.
Save anujsinghwd/409d0c0fca67806e7bd9024b6d0d63f7 to your computer and use it in GitHub Desktop.
Loading map with Mapbox GL JS + React JS
.map{
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
import React, { Component } from 'react';
import mapboxgl from 'mapbox-gl';
import './mapload.css';
mapboxgl.accessToken = 'Your-Mapbox-Token';
var map;
class MapLoad extends Component {
constructor(props) {
super(props);
this.state = {
lat: 27.85380233830591,
lng: 78.37183893820759,
zoom: 8.5
};
}
componentDidMount() {
const { lat, lng, zoom } = this.state;
map = new mapboxgl.Map({
container: this.mapDiv,
style: 'mapbox://styles/mapbox/streets-v9',
center: [lng, lat],
zoom: zoom
});
}
render() {
return (
<div>
<div ref={e => this.mapDiv = e} className="map" />
</div>
);
}
}
export default MapLoad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment