-
Notifications
You must be signed in to change notification settings - Fork 3
/
GLMap.js
65 lines (56 loc) · 1.52 KB
/
GLMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { Component } from 'react';
// NotWorkingShimInstead import mapboxgl from 'mapbox-gl';
require('script!mapbox-gl/dist/mapbox-gl-dev.js');
class GLMap extends Component {
static propTypes = {
// Default map view
view: React.PropTypes.object,
// Style of map container
mapStyle: React.PropTypes.object,
// Current base layer
baselayer: React.PropTypes.string,
// Mapbox map token
token: React.PropTypes.string,
// onStyleEvent fired after style loaded. Map object is passed
onStyleLoad: React.PropTypes.func
}
componentDidMount() {
mapboxgl.accessToken = this.props.token;
this.map = new mapboxgl.Map(this.props.view);
this.map.on('style.load', () => {
this.map.addSource('satellite', {
type: 'raster',
url: 'mapbox://mapbox.satellite'
});
this.map.addLayer({
'id': 'satellite',
'type': 'raster',
'source': 'satellite',
'layout': {
'visibility': 'none'
}
});
if (this.props.onStyleLoad) {
this.props.onStyleLoad(this.map);
}
});
}
componentDidUpdate() {
if (this.props.baselayer === 'satellite') {
this.map.setLayoutProperty('satellite', 'visibility', 'visible');
} else {
this.map.setLayoutProperty('satellite', 'visibility', 'none');
}
}
componentWillUnmount() {
this.map.remove();
}
render() {
return (
<div>
<div style={this.props.mapStyle} id='map'></div>
</div>
);
}
}
export default GLMap;