GoogleMap

Storybook

Open the GoogleMap story

GoogleMap creates a google.maps.Map and provides it to its children. The camera (center, zoom, heading, tilt) is controlled by props and can be two-way bound, so user panning and zooming flows back into your state.

Props

PropTypeDefaultDescription
centergoogle.maps.LatLngLiteral | google.maps.LatLngMap center. Bindable: use bind:center to track panning.
zoomnumberZoom level. Bindable: use bind:zoom to track zooming.
headingnumberHeading in degrees. Bindable.
tiltnumberTilt in degrees. Bindable.
mapIdstringMap ID from the Cloud Console. Required for AdvancedMarker. Creation-only.
colorSchemegoogle.maps.MapOptions['colorScheme']LIGHT, DARK or FOLLOW_SYSTEM. Creation-only.
renderingTypegoogle.maps.MapOptions['renderingType']RASTER or VECTOR. Creation-only.
optionsgoogle.maps.MapOptions (without camera, mapId, colorScheme, renderingType)Any other map options. Applied with setOptions() whenever it changes.
idstringid of the map container.
classstring''Class of the map container.
stylestring'width:100%;height:100%;'Inline style of the map container. The container needs a height.
mapgoogle.maps.Map | nullnullThe map instance. Bindable: read it with bind:map.
childrenSnippetMap children, rendered once the map exists.

Event Props

PropTypeDescription
onClick(event: google.maps.MapMouseEvent) => voidclick
onDblClick(event: google.maps.MapMouseEvent) => voiddblclick
onContextMenu(event: google.maps.MapMouseEvent) => voidcontextmenu (right click)
onMouseMove(event: google.maps.MapMouseEvent) => voidmousemove
onMouseOver(event: google.maps.MapMouseEvent) => voidmouseover
onMouseOut(event: google.maps.MapMouseEvent) => voidmouseout
onDragStart() => voiddragstart
onDrag() => voiddrag
onDragEnd() => voiddragend
onIdle(map: google.maps.Map) => voididle
onTilesLoaded(map: google.maps.Map) => voidtilesloaded
onBoundsChanged(bounds: google.maps.LatLngBoundsLiteral) => voidbounds_changed
onCameraChanged(camera: MapCameraState) => voidAny of center_changed, zoom_changed, heading_changed, tilt_changed. Receives center, zoom, heading and tilt.
onLoad(map: google.maps.Map) => voidCalled once after the map is created.

Usage

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

	let center = $state({ lat: 35.6812, lng: 139.7671 });
	let zoom = $state(12);
</script>

<div style="height: 400px;">
	<APIProvider apiKey="YOUR_API_KEY">
		<GoogleMap
			bind:center
			bind:zoom
			mapId="DEMO_MAP_ID"
			options={{ disableDefaultUI: true, gestureHandling: 'greedy' }}
			onClick={(event) => console.log(event.latLng?.toJSON())}
		/>
	</APIProvider>
</div>

<p>{center.lat.toFixed(4)}, {center.lng.toFixed(4)} @ {zoom}</p>
svelte

Migrating from 0.2

  • options.center and options.zoom are now the center and zoom props (bindable).
  • mapContainerStyle is now style, and mapContainerClassName is now class.
  • options.mapId is now the mapId prop.
  • onLoad is unchanged. onUnmount was removed; use an effect cleanup in a child component if you need it.
  • The map instance is also available with bind:map or the useMap() hook.

Notes

  • The map is invisible until it has both center and zoom.
  • mapId, colorScheme and renderingType are read once when the map is created. Recreate the component (for example inside a key block) to change them.
  • Children are rendered only after the map exists, so child components can use the map right away.