diff --git a/examples/markers-and-infowindows/assets/info-circle.svg b/examples/markers-and-infowindows/assets/info-circle.svg new file mode 100644 index 00000000..8728591d --- /dev/null +++ b/examples/markers-and-infowindows/assets/info-circle.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/examples/markers-and-infowindows/src/app.tsx b/examples/markers-and-infowindows/src/app.tsx index 01c4192e..5b426b67 100644 --- a/examples/markers-and-infowindows/src/app.tsx +++ b/examples/markers-and-infowindows/src/app.tsx @@ -43,6 +43,19 @@ const App = () => { glyphColor={'#0f677a'}> + {/* advanced marker with html pin glyph */} + + + {/* child gets rendered as 'glyph' element of pin */} + + + + {/* advanced marker with html-content */} { +export const Pin = (props: PropsWithChildren) => { const advancedMarker = useContext(AdvancedMarkerContext)?.marker; + const glyphContainer = useMemo(() => document.createElement('div'), []); // Create Pin View instance useEffect(() => { @@ -24,15 +33,33 @@ export const Pin = (props: PinProps) => { return; } + if (props.glyph && props.children) { + logErrorOnce( + 'The component only uses children to render the glyph if both the glyph property and children are present.' + ); + } + + if (Children.count(props.children) > 1) { + logErrorOnce( + 'Passing multiple children to the component might lead to unexpected results.' + ); + } + const pinViewOptions: google.maps.marker.PinElementOptions = { ...props }; const pinElement = new google.maps.marker.PinElement(pinViewOptions); + // Set glyph to glyph container if children are present (rendered via portal). + // If both props.glyph and props.children are present, props.children takes priority. + if (props.children) { + pinElement.glyph = glyphContainer; + } + // Set content of Advanced Marker View to the Pin View element advancedMarker.content = pinElement.element; }, [advancedMarker, props]); - return null; + return createPortal(props.children, glyphContainer); };