Skip to content

Instantly share code, notes, and snippets.

@gnzandrs
Created February 10, 2019 02:59
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 gnzandrs/b39b80bab8a7a5a340a7cbde3a0dc967 to your computer and use it in GitHub Desktop.
Save gnzandrs/b39b80bab8a7a5a340a7cbde3a0dc967 to your computer and use it in GitHub Desktop.
mapbox rendering with react and node client-side for medium
import React from 'react'
let mapboxgl;
if (__CLIENT__) {
mapboxgl = require('mapbox-gl')
mapboxgl.accessToken = 'yourkeyhere';
}
class Map extends React.Component {
constructor() {
super()
this.state = {
width: 300,
height: 300
}
this.mapContainer = null
this.lat = -70.6244855
this.long = -33.4240608
}
componentDidMount() {
const map = new mapboxgl.Map({
container: this.mapContainer,
style: 'mapbox://styles/mapbox/streets-v9',
center: [this.lat, this.long],
zoom: 10
});
var marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat([this.lat, this.long])
.addTo(map);
marker.on('dragend', function () {
var lngLat = marker.getLngLat()
})
}
render() {
const divStyle = {
width: this.state.width,
height: this.state.height
}
return (
<div>
<div ref={el => this.mapContainer = el} style={divStyle} />
</div>
)
}
}
export default Map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment