1- import { type ContextType , PureComponent } from 'react'
1+ import { type ContextType , PureComponent , memo , useMemo , useEffect , useContext } from 'react'
22
33import invariant from 'invariant'
44
@@ -27,9 +27,9 @@ export interface GroundOverlayProps {
2727 /** The opacity of the overlay, expressed as a number between 0 and 1. Optional. Defaults to 1. */
2828 opacity ?: number | undefined
2929 /** 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
3131 /** 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
3333 /** The url of the projected image */
3434 url : string
3535 /** The bounds that the image will be scaled to fit */
@@ -38,8 +38,60 @@ export interface GroundOverlayProps {
3838 onLoad ?: ( ( groundOverlay : google . maps . GroundOverlay ) => void ) | undefined
3939 /** This callback is called when the component unmounts. It is called with the groundOverlay instance. */
4040 onUnmount ?: ( ( groundOverlay : google . maps . GroundOverlay ) => void ) | undefined
41+ visible ?: boolean
4142}
4243
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+
4395export class GroundOverlay extends PureComponent < GroundOverlayProps , GroundOverlayState > {
4496 public static defaultProps = {
4597 onLoad : noop ,
0 commit comments