1
- import { type ContextType , PureComponent } from 'react'
1
+ import { type ContextType , PureComponent , memo , useMemo , useEffect , useContext } from 'react'
2
2
3
3
import invariant from 'invariant'
4
4
@@ -27,9 +27,9 @@ export interface GroundOverlayProps {
27
27
/** The opacity of the overlay, expressed as a number between 0 and 1. Optional. Defaults to 1. */
28
28
opacity ?: number | undefined
29
29
/** This event is fired when the DOM dblclick event is fired on the GroundOverlay. */
30
- onDblClick ?: ( ( e : google . maps . MapMouseEvent ) => void ) | undefined
30
+ onDblClick ?: ( ( e : google . maps . MapMouseEvent ) => void ) | undefined
31
31
/** This event is fired when the DOM click event is fired on the GroundOverlay. */
32
- onClick ?: ( ( e : google . maps . MapMouseEvent ) => void ) | undefined
32
+ onClick ?: ( ( e : google . maps . MapMouseEvent ) => void ) | undefined
33
33
/** The url of the projected image */
34
34
url : string
35
35
/** The bounds that the image will be scaled to fit */
@@ -38,8 +38,60 @@ export interface GroundOverlayProps {
38
38
onLoad ?: ( ( groundOverlay : google . maps . GroundOverlay ) => void ) | undefined
39
39
/** This callback is called when the component unmounts. It is called with the groundOverlay instance. */
40
40
onUnmount ?: ( ( groundOverlay : google . maps . GroundOverlay ) => void ) | undefined
41
+ visible ?: boolean
41
42
}
42
43
44
+ function GroundOverlayFunctional ( { url, bounds, options, visible } : GroundOverlayProps ) {
45
+ const map = useContext < google . maps . Map | null > ( MapContext )
46
+
47
+ const imageBounds = new google . maps . LatLngBounds (
48
+ new google . maps . LatLng ( bounds . south , bounds . west ) ,
49
+ new google . maps . LatLng ( bounds . north , bounds . east )
50
+ ) ;
51
+
52
+ const groundOverlay = useMemo ( ( ) => {
53
+ const overlay = new google . maps . GroundOverlay ( url , imageBounds , {
54
+ ...options
55
+ } ) ;
56
+ return overlay
57
+ } , [ ] ) ;
58
+
59
+ useEffect ( ( ) => {
60
+ if ( groundOverlay !== null ) {
61
+ groundOverlay . setMap ( map ) ;
62
+ }
63
+ } , [ map ] )
64
+
65
+ useEffect ( ( ) => {
66
+ if ( typeof url !== 'undefined' && groundOverlay !== null ) {
67
+ groundOverlay . set ( "url" , url ) ;
68
+ groundOverlay . setMap ( map ) ;
69
+ }
70
+ } , [ groundOverlay , url ] ) ;
71
+
72
+ useEffect ( ( ) => {
73
+ if ( typeof visible !== 'undefined' && groundOverlay !== null ) {
74
+ groundOverlay . setOpacity ( visible ? 1 : 0 ) ;
75
+ }
76
+ } , [ groundOverlay , visible ] ) ;
77
+
78
+ useEffect ( ( ) => {
79
+ const newBounds = new google . maps . LatLngBounds (
80
+ new google . maps . LatLng ( bounds . south , bounds . west ) ,
81
+ new google . maps . LatLng ( bounds . north , bounds . east )
82
+ ) ;
83
+
84
+ if ( typeof bounds !== 'undefined' && groundOverlay !== null ) {
85
+ groundOverlay . set ( "bounds" , newBounds ) ;
86
+ groundOverlay . setMap ( map ) ;
87
+ }
88
+ } , [ groundOverlay , bounds ] )
89
+
90
+ return null ;
91
+ }
92
+
93
+ export const GroundOverlayF = memo ( GroundOverlayFunctional ) ;
94
+
43
95
export class GroundOverlay extends PureComponent < GroundOverlayProps , GroundOverlayState > {
44
96
public static defaultProps = {
45
97
onLoad : noop ,
0 commit comments