forked from fullstackreact/google-maps-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatMap.js
More file actions
106 lines (82 loc) · 2.27 KB
/
HeatMap.js
File metadata and controls
106 lines (82 loc) · 2.27 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react'
import PropTypes from 'prop-types'
import { camelize } from '../lib/String'
const evtNames = ['click', 'mouseover', 'recenter'];
const wrappedPromise = function() {
var wrappedPromise = {},
promise = new Promise(function (resolve, reject) {
wrappedPromise.resolve = resolve;
wrappedPromise.reject = reject;
});
wrappedPromise.then = promise.then.bind(promise);
wrappedPromise.catch = promise.catch.bind(promise);
wrappedPromise.promise = promise;
return wrappedPromise;
}
export class HeatMap extends React.Component {
componentDidMount() {
this.heatMapPromise = wrappedPromise();
this.renderHeatMap();
}
componentDidUpdate(prevProps) {
if ((this.props.map !== prevProps.map) ||
(this.props.position !== prevProps.position)) {
if (this.heatMap) {
this.heatMap.setMap(null);
this.renderHeatMap();
}
}
}
componentWillUnmount() {
if (this.heatMap) {
this.heatMap.setMap(null);
}
}
renderHeatMap() {
let {
map, google, positions, mapCenter, icon, gradient, radius, opacity
} = this.props;
if (!google) {
return null;
}
positions = positions.map((pos) => {
return new google.maps.LatLng(pos.lat, pos.lng);
});
const pref = {
map: map,
data: positions,
};
this.heatMap = new google.maps.visualization.HeatmapLayer(pref);
this.heatMap.set('gradient', gradient);
this.heatMap.set('radius', radius === undefined ? 20 : radius);
this.heatMap.set('opacity', opacity === undefined ? 0.2 : opacity);
evtNames.forEach(e => {
this.heatMap.addListener(e, this.handleEvent(e));
});
this.heatMapPromise.resolve(this.heatMap);
}
getHeatMap() {
return this.heatMapPromise;
}
handleEvent(evt) {
return (e) => {
const evtName = `on${camelize(evt)}`
if (this.props[evtName]) {
this.props[evtName](this.props, this.heatMap, e);
}
}
}
render() {
return null;
}
}
HeatMap.propTypes = {
position: PropTypes.object,
map: PropTypes.object,
icon: PropTypes.string
}
evtNames.forEach(e => HeatMap.propTypes[e] = PropTypes.func)
HeatMap.defaultProps = {
name: 'HeatMap'
}
export default HeatMap