|
| 1 | +import React, { useRef, useEffect, useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +function createPopupClass() { |
| 5 | + function Popup({ position, content, map, passThroughMouseEvents, onDraw }) { |
| 6 | + this.position = position; |
| 7 | + this.containerDiv = content; |
| 8 | + this.onDraw = onDraw; |
| 9 | + this.setMap(map); |
| 10 | + if (!passThroughMouseEvents) { |
| 11 | + google.maps.OverlayView.preventMapHitsAndGesturesFrom(this.containerDiv); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + Popup.prototype = Object.create(google.maps.OverlayView.prototype); |
| 16 | + |
| 17 | + Popup.prototype.show = function () { |
| 18 | + this.containerDiv.style.visibility = 'visible'; |
| 19 | + }; |
| 20 | + |
| 21 | + Popup.prototype.hide = function () { |
| 22 | + this.containerDiv.style.visibility = 'hidden'; |
| 23 | + }; |
| 24 | + |
| 25 | + Popup.prototype.onAdd = function () { |
| 26 | + this.getPanes().floatPane.appendChild(this.containerDiv); |
| 27 | + }; |
| 28 | + |
| 29 | + Popup.prototype.onRemove = function () { |
| 30 | + if (this.containerDiv.parentElement) { |
| 31 | + this.containerDiv.parentElement.removeChild(this.containerDiv); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + Popup.prototype.draw = function () { |
| 36 | + if (!this.position) { |
| 37 | + return; |
| 38 | + } |
| 39 | + this.onDraw(); |
| 40 | + var divPosition = this.getProjection().fromLatLngToDivPixel(this.position); |
| 41 | + var display = |
| 42 | + Math.abs(divPosition.x) < 4000 && Math.abs(divPosition.y) < 4000 |
| 43 | + ? 'block' |
| 44 | + : 'none'; |
| 45 | + |
| 46 | + if (display === 'block') { |
| 47 | + this.containerDiv.style.left = divPosition.x + 'px'; |
| 48 | + this.containerDiv.style.top = divPosition.y + 'px'; |
| 49 | + } |
| 50 | + if (this.containerDiv.style.display !== display) { |
| 51 | + this.containerDiv.style.display = display; |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + return Popup; |
| 56 | +} |
| 57 | + |
| 58 | +const asLatLng = (position) => |
| 59 | + !position || position instanceof google.maps.LatLng |
| 60 | + ? position |
| 61 | + : new google.maps.LatLng(position.lat, position.lng); |
| 62 | + |
| 63 | +export const CustomOverlay = ({ |
| 64 | + map, |
| 65 | + position, |
| 66 | + children, |
| 67 | + visible, |
| 68 | + className, |
| 69 | + passThroughMouseEvents |
| 70 | +}) => { |
| 71 | + const [hasDrawn, setHasDrawn] = useState(false); |
| 72 | + const containerRef = useRef(null); |
| 73 | + const popoverRef = useRef(null); |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + if (map) { |
| 77 | + const Popup = createPopupClass(); |
| 78 | + popoverRef.current = new Popup({ |
| 79 | + position: asLatLng(position), |
| 80 | + content: containerRef.current, |
| 81 | + map, |
| 82 | + passThroughMouseEvents, |
| 83 | + onDraw: () => setHasDrawn(true) |
| 84 | + }); |
| 85 | + } |
| 86 | + }, [map]); |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + const popover = popoverRef.current; |
| 90 | + if (popover) { |
| 91 | + popover.position = asLatLng(position); |
| 92 | + popover.draw(); |
| 93 | + } |
| 94 | + }, [position]); |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + const popover = popoverRef.current; |
| 98 | + if (popover) { |
| 99 | + visible ? popover.show() : popover.hide(); |
| 100 | + } |
| 101 | + }, [visible]); |
| 102 | + |
| 103 | + const display = hasDrawn ? 'block' : 'none'; |
| 104 | + return ( |
| 105 | + <div |
| 106 | + className={className} |
| 107 | + style={{ position: 'absolute', display }} |
| 108 | + ref={containerRef} |
| 109 | + > |
| 110 | + {visible && children} |
| 111 | + </div> |
| 112 | + ); |
| 113 | +}; |
| 114 | + |
| 115 | +CustomOverlay.propTypes = { |
| 116 | + className: PropTypes.string, |
| 117 | + children: PropTypes.node.isRequired, |
| 118 | + map: PropTypes.object, |
| 119 | + position: PropTypes.object, |
| 120 | + visible: PropTypes.bool, |
| 121 | + passThroughMouseEvents: PropTypes.bool |
| 122 | +}; |
| 123 | + |
| 124 | +CustomOverlay.defaultProps = { |
| 125 | + visible: true |
| 126 | +}; |
| 127 | + |
| 128 | +export default CustomOverlay; |
0 commit comments