Created
February 10, 2019 02:59
-
-
Save gnzandrs/b39b80bab8a7a5a340a7cbde3a0dc967 to your computer and use it in GitHub Desktop.
mapbox rendering with react and node client-side for medium
This file contains hidden or 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 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