2016-11-25 13:31:41 +01:00
|
|
|
import React from 'react'
|
2016-12-19 12:51:23 +01:00
|
|
|
import MapboxGl from 'mapbox-gl'
|
2016-11-25 13:31:41 +01:00
|
|
|
import validateColor from 'mapbox-gl-style-spec/lib/validate/validate_color'
|
|
|
|
|
2016-12-20 11:44:22 +01:00
|
|
|
import Map from './Map.jsx'
|
|
|
|
import style from '../../libs/style.js'
|
|
|
|
|
|
|
|
export default class MapboxGlMap extends Map {
|
2016-12-19 21:21:10 +01:00
|
|
|
static propTypes = {
|
|
|
|
onMapLoaded: React.PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
onMapLoaded: () => {}
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:52:04 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { map: null }
|
|
|
|
}
|
2016-11-25 13:31:41 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-12-20 13:28:50 +01:00
|
|
|
if(!this.state.map) return
|
2016-11-25 13:31:41 +01:00
|
|
|
|
2016-12-20 13:28:50 +01:00
|
|
|
//Mapbox GL now does diffing natively so we don't need to calculate
|
|
|
|
//the necessary operations ourselves!
|
2016-12-20 16:08:49 +01:00
|
|
|
this.state.map.setStyle(nextProps.mapStyle, { diff: true})
|
2016-11-25 13:31:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
MapboxGl.accessToken = this.props.accessToken
|
|
|
|
|
|
|
|
const map = new MapboxGl.Map({
|
|
|
|
container: this.container,
|
2016-12-20 16:08:49 +01:00
|
|
|
style: this.props.mapStyle,
|
2016-11-25 13:31:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
map.on("style.load", (...args) => {
|
2016-12-19 21:21:10 +01:00
|
|
|
this.props.onMapLoaded(map)
|
2016-11-25 13:31:41 +01:00
|
|
|
this.setState({ map });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|