-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathMarker.js
More file actions
123 lines (103 loc) · 2.37 KB
/
Marker.js
File metadata and controls
123 lines (103 loc) · 2.37 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React from 'react'
import PropTypes from 'prop-types'
import { camelize } from '../lib/String'
import { verifyMarkerPosition } from '../lib/verifyMarkerPosition'
const evtNames = [
'click',
'dblclick',
'dragend',
'mousedown',
'mouseout',
'mouseover',
'mouseup',
'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 Marker extends React.Component {
componentDidMount() {
this.markerPromise = wrappedPromise();
this.renderMarker();
}
componentDidUpdate(prevProps) {
if (this.props.map !== prevProps.map ||
this.props.icon !== prevProps.icon ||
verifyMarkerPosition(this.props.position, prevProps.position)) {
if (this.marker) {
this.marker.setMap(null);
}
this.renderMarker();
}
}
componentWillUnmount() {
if (this.marker) {
this.marker.setMap(null);
}
}
renderMarker() {
const {
map,
google,
position,
mapCenter,
icon,
label,
draggable,
title,
...props
} = this.props;
if (!google) {
return null
}
let pos = position || mapCenter;
if (!(pos instanceof google.maps.LatLng)) {
pos = new google.maps.LatLng(pos.lat, pos.lng);
}
const pref = {
map,
position: pos,
icon,
label,
title,
draggable,
...props
};
this.marker = new google.maps.Marker(pref);
evtNames.forEach(e => {
this.marker.addListener(e, this.handleEvent(e));
});
this.markerPromise.resolve(this.marker);
}
getMarker() {
return this.markerPromise;
}
handleEvent(evt) {
return (e) => {
const evtName = `on${camelize(evt)}`
if (this.props[evtName]) {
this.props[evtName](this.props, this.marker, e);
}
}
}
render() {
return null;
}
}
Marker.propTypes = {
position: PropTypes.object,
map: PropTypes.object
}
evtNames.forEach(e => Marker.propTypes[e] = PropTypes.func)
Marker.defaultProps = {
name: 'Marker'
}
export default Marker