Last active
August 22, 2019 06:06
-
-
Save anujsinghwd/409d0c0fca67806e7bd9024b6d0d63f7 to your computer and use it in GitHub Desktop.
Loading map with Mapbox GL JS + React JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.map{ | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
width: 100%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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