InfoWindow

Storybook

Open the InfoWindow story

InfoWindow shows a google.maps.InfoWindow popup, either anchored to a marker or at a position. Its content and optional header are Svelte snippets, and its open state can be two-way bound.

Props

PropTypeDefaultDescription
openbooleantrueWhether the info window is open. Bindable: closing it from the map sets it to false.
anchorgoogle.maps.marker.AdvancedMarkerElement | google.maps.MVCObject | nullMarker to attach to. Takes precedence over position.
positionLatLngInput | nullPosition used when there is no anchor.
headerContentstringHeader text. Use the header snippet for rich content.
headerDisabledbooleanHides the header row, including the close button.
maxWidthnumberMaximum width in pixels.
minWidthnumberMinimum width in pixels.
pixelOffset[number, number]Offset from the anchor point, as [x, y].
zIndexnumberStacking order.
disableAutoPanbooleanStops the map from panning to show the info window when it opens.
ariaLabelstringAccessible label.
shouldFocusbooleanWhether focus moves into the info window when it opens.
optionsgoogle.maps.InfoWindowOptionsOther info window options.
infoWindowgoogle.maps.InfoWindow | nullnullThe instance. Bindable: read it with bind:infoWindow.
headerSnippetRich header content. Overrides headerContent when it renders something.
childrenSnippetInfo window content.

Event Props

PropTypeDescription
onClose() => voidCalled whenever the info window closes (close button or open = false).
onCloseClick() => voidCalled when the user clicks the close button.
onLoad(infoWindow: google.maps.InfoWindow) => voidCalled once after the info window is created.

Usage

<script lang="ts">
	import { APIProvider, GoogleMap, AdvancedMarker, InfoWindow } from 'svelte-google-maps-api';

	let marker = $state<google.maps.marker.AdvancedMarkerElement | null>(null);
	let open = $state(false);
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap center={{ lat: 35.6812, lng: 139.7671 }} zoom={13} mapId="DEMO_MAP_ID">
			<AdvancedMarker
				position={{ lat: 35.6812, lng: 139.7671 }}
				bind:marker
				onClick={() => (open = true)}
			/>

			{#if marker}
				<InfoWindow anchor={marker} bind:open maxWidth={240}>
					{#snippet header()}
						<strong>Tokyo Station</strong>
					{/snippet}
					<p>Opened in 1914. Marunouchi side.</p>
				</InfoWindow>
			{/if}

			<InfoWindow position={{ lat: 35.6586, lng: 139.7454 }} headerContent="Tokyo Tower">
				<p>333 m tall.</p>
			</InfoWindow>
		</GoogleMap>
	</APIProvider>
</div>
svelte

Notes

  • Get the marker for anchor with bind:marker on AdvancedMarker. Render the InfoWindow only once the marker exists.
  • Unmounting the component closes the info window.
  • InfoWindow must be inside GoogleMap.