APIProvider

Storybook

Open the APIProvider story

APIProvider loads the Google Maps JavaScript API once per page and makes it available to every component inside it. Children are rendered only after the core and maps libraries are ready; other libraries (marker, places, routes, streetView, ...) are loaded on demand by the components that need them.

Props

PropTypeDefaultDescription
apiKeystring(required)Google Maps Platform API key.
versionstring'weekly'Maps JavaScript API version or channel (weekly, quarterly, beta, 3.66, ...).
librariesLibrary[][]Libraries to preload. Components load the libraries they need on demand, so this is optional.
languagestringLanguage for map labels and controls.
regionstringRegion code used to bias results.
authReferrerPolicy'origin'Sent as auth_referrer_policy.
channelstringUsage tracking channel.
solutionChannelstringSent as solution_channel.
mapIdsstring[]Map IDs to preload.
apiUrlstring'https://maps.googleapis.com'Base URL of the Maps JavaScript API.
noncestringCSP nonce for the injected script tag.
preventGoogleFontsLoadingbooleanfalseStops the API from injecting Google Fonts (Roboto / Google Sans Text).
childrenSnippetContent rendered once the API is ready.
loadingSnippetRendered while the API loads.
errorSnippet (receives the Error)Rendered when the API fails to load. Without it, a short message with role="alert" is shown.

Event Props

PropTypeDescription
onLoad() => voidCalled after the script and the preloaded libraries are ready.
onError(error: Error) => voidCalled when the script or a preloaded library fails to load, or when apiKey is empty.

Usage

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

	const apiKey = 'YOUR_API_KEY';
</script>

<div style="height: 400px;">
	<APIProvider {apiKey} language="ja" region="JP">
		<GoogleMap center={{ lat: 35.6812, lng: 139.7671 }} zoom={12} />

		{#snippet loading()}
			<p>Loading map...</p>
		{/snippet}

		{#snippet error(err)}
			<p>Could not load Google Maps: {err.message}</p>
		{/snippet}
	</APIProvider>
</div>
svelte

Hooks

These helpers must be called during component initialisation inside APIProvider:

  • useAPILoadingStatus() returns an object with reactive status ('loading' | 'loaded' | 'error') and error.
  • useMapsLibrary(name) loads a library with importLibrary() and returns an object whose current is the library, or null until it loads.
  • useMap() returns an object whose current is the nearest GoogleMap instance (call it inside GoogleMap).
<script lang="ts">
	import { useMap, useMapsLibrary } from 'svelte-google-maps-api';

	const map = useMap();
	const geometry = useMapsLibrary('geometry');

	$effect(() => {
		if (map.current && geometry.current) {
			// Use map.current and geometry.current here.
		}
	});
</script>
svelte

See Getting Started for a full walkthrough.

Notes

  • The Maps JavaScript API can only be loaded once per page. If a second APIProvider uses different options, the first load is reused and a warning is logged.
  • If the API was already loaded by another script tag, APIProvider reuses it instead of injecting a new one.
  • Nothing is loaded during server-side rendering; the loading snippet is rendered on the server.