maputnik/src/map.jsx

41 lines
1,015 B
React
Raw Normal View History

import React from 'react'
2016-09-10 00:43:41 +02:00
import MapboxGl from 'mapbox-gl';
import diffStyles from 'mapbox-gl-style-spec/lib/diff'
2016-09-09 11:29:18 +02:00
import theme from './theme.js'
2016-09-08 19:47:29 +02:00
export class Map extends React.Component {
2016-09-09 15:49:23 +02:00
static propTypes = {
2016-09-09 23:25:06 +02:00
mapStyle: React.PropTypes.object.isRequired
2016-09-09 15:49:23 +02:00
}
2016-09-10 00:43:41 +02:00
componentWillReceiveProps(nextProps) {
const map = this.state.map
if(map) {
const changes = diffStyles(this.props.mapStyle.toJS(), nextProps.mapStyle.toJS())
2016-09-10 00:43:41 +02:00
changes.forEach(change => {
map[change.command].apply(map, change.args);
});
}
}
shouldComponentUpdate(nextProps, nextState) {
return nextProps.mapStyle !== this.props.mapStyle
}
componentDidMount() {
MapboxGl.accessToken = "pk.eyJ1IjoibW9yZ2Vua2FmZmVlIiwiYSI6IjIzcmN0NlkifQ.0LRTNgCc-envt9d5MzR75w";
const map = new MapboxGl.Map({
container: this.container,
style: this.props.mapStyle.toJS(),
2016-09-10 00:43:41 +02:00
});
map.on("style.load", (...args) => {
this.setState({ map });
});
}
2016-09-08 19:47:29 +02:00
render() {
2016-09-10 00:43:41 +02:00
return <div ref={x => this.container = x}></div>
2016-09-08 19:47:29 +02:00
}
}