Created
January 19, 2023 16:44
-
-
Save pjflanagan/ca1339f4be5432b97f4ddee5f4371642 to your computer and use it in GitHub Desktop.
Google Maps Overlay View with React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PopupConfig = { | |
map: google.maps.Map; | |
content: React.Component; | |
position: google.maps.LatLng; | |
} | |
class Popup extends google.maps.OverlayView { | |
private readonly container: HTMLDivElement; | |
private content: ComponentChild; | |
private position: google.maps.LatLng; | |
constructor({ map, content, position }: PopupConfig) { | |
super(); | |
// The container is a div that we can feed to Google Maps and attach our React content to | |
this.container = document.createElement('div'); | |
this.container.style.position = 'absolute'; | |
this.setMap(map); | |
this.content = content; | |
} | |
public onAdd(): void { | |
this.getPanes().floatPane.appendChild(this.container); | |
} | |
public draw(): void { | |
// The draw function is called every time the Popup needs to render | |
// so here we will use React to render our content (React.Component) | |
React.render(this.content, this.container); | |
} | |
public onRemove(): void { | |
if (this.container.parentElement) { | |
this.container.parentElement.removeChild(this.container); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment