Skip to content

Instantly share code, notes, and snippets.

@pjflanagan
Created January 19, 2023 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjflanagan/ca1339f4be5432b97f4ddee5f4371642 to your computer and use it in GitHub Desktop.
Save pjflanagan/ca1339f4be5432b97f4ddee5f4371642 to your computer and use it in GitHub Desktop.
Google Maps Overlay View with React
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