-
Notifications
You must be signed in to change notification settings - Fork 816
/
Copy pathCustomOverlay.js
128 lines (110 loc) · 3.07 KB
/
CustomOverlay.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
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
124
125
126
127
128
import React, { useRef, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
function createPopupClass() {
function Popup({ position, content, map, passThroughMouseEvents, onDraw }) {
this.position = position;
this.containerDiv = content;
this.onDraw = onDraw;
this.setMap(map);
if (!passThroughMouseEvents) {
google.maps.OverlayView.preventMapHitsAndGesturesFrom(this.containerDiv);
}
}
Popup.prototype = Object.create(google.maps.OverlayView.prototype);
Popup.prototype.show = function () {
this.containerDiv.style.visibility = 'visible';
};
Popup.prototype.hide = function () {
this.containerDiv.style.visibility = 'hidden';
};
Popup.prototype.onAdd = function () {
this.getPanes().floatPane.appendChild(this.containerDiv);
};
Popup.prototype.onRemove = function () {
if (this.containerDiv.parentElement) {
this.containerDiv.parentElement.removeChild(this.containerDiv);
}
};
Popup.prototype.draw = function () {
if (!this.position) {
return;
}
this.onDraw();
var divPosition = this.getProjection().fromLatLngToDivPixel(this.position);
var display =
Math.abs(divPosition.x) < 4000 && Math.abs(divPosition.y) < 4000
? 'block'
: 'none';
if (display === 'block') {
this.containerDiv.style.left = divPosition.x + 'px';
this.containerDiv.style.top = divPosition.y + 'px';
}
if (this.containerDiv.style.display !== display) {
this.containerDiv.style.display = display;
}
};
return Popup;
}
const asLatLng = (position) =>
!position || position instanceof google.maps.LatLng
? position
: new google.maps.LatLng(position.lat, position.lng);
export const CustomOverlay = ({
map,
position,
children,
visible,
className,
passThroughMouseEvents
}) => {
const [hasDrawn, setHasDrawn] = useState(false);
const containerRef = useRef(null);
const popoverRef = useRef(null);
useEffect(() => {
if (map) {
const Popup = createPopupClass();
popoverRef.current = new Popup({
position: asLatLng(position),
content: containerRef.current,
map,
passThroughMouseEvents,
onDraw: () => setHasDrawn(true)
});
}
}, [map]);
useEffect(() => {
const popover = popoverRef.current;
if (popover) {
popover.position = asLatLng(position);
popover.draw();
}
}, [position]);
useEffect(() => {
const popover = popoverRef.current;
if (popover) {
visible ? popover.show() : popover.hide();
}
}, [visible]);
const display = hasDrawn ? 'block' : 'none';
return (
<div
className={className}
style={{ position: 'absolute', display }}
ref={containerRef}
>
{visible && children}
</div>
);
};
CustomOverlay.propTypes = {
className: PropTypes.string,
children: PropTypes.node.isRequired,
map: PropTypes.object,
position: PropTypes.object,
visible: PropTypes.bool,
passThroughMouseEvents: PropTypes.bool
};
CustomOverlay.defaultProps = {
visible: true
};
export default CustomOverlay;