APIProvider

Storybook

Open the APIProvider story

The APIProvider component is responsible for loading the Google Maps JavaScript API.

Props

PropTypeDefaultDescription
apiKeystring''Google Maps API key.
googleMapsApiKeystringAPI key alias.
googleMapsClientIdstringGoogle Maps Premium Plan client id. Do not use with an API key.
librariesLibrary[][]Google Maps libraries to load.
versionstring'weekly'Google Maps API version to load.
languagestringLanguage for the Google Maps API.
regionstringRegion for the Google Maps API.
channelstringUsage tracking channel.
mapIdsstring[]Map IDs for cloud-based map styling.
authReferrerPolicy'origin'Auth referrer policy.
apiUrlstring'https://maps.googleapis.com'Google Maps API base URL.
solutionChannelstringGoogle solution channel.
idstring'svelte-google-maps-api-script'Script element id.
noncestringCSP nonce for the script tag.
preventGoogleFontsLoadingbooleanfalsePrevents Google Maps from injecting its default font styles.
onLoad() => voidCalled when the script has loaded.
onError(error: Error) => voidCalled when the script fails to load.
onUnmount() => voidCalled when the provider is destroyed.

Usage

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

	const apiKey = 'YOUR_API_KEY';
	const libraries = ['places', 'geometry'];
</script>

<APIProvider {apiKey} {libraries}>
	<GoogleMap id="map" options={{ center: { lat: 35.6812362, lng: 139.7649361 }, zoom: 10 }} />
</APIProvider>
svelte

Context

The APIProvider component sets the svelte-google-maps-api context with the load status, API reference, and error state. GoogleMap and service components use that context to wait for the script before creating Google Maps objects.

Loading Multiple Libraries

You can load additional Google Maps libraries by passing them in the libraries prop. If the Google Maps script is already present, APIProvider imports the requested libraries before rendering child components:

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

	const apiKey = 'YOUR_API_KEY';
	const libraries = ['places', 'geometry', 'drawing', 'visualization'];
</script>

<APIProvider {apiKey} {libraries}>
	<!-- Your map components here -->
</APIProvider>
svelte

Language and Region

You can specify the language and region for the Google Maps API:

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

	const apiKey = 'YOUR_API_KEY';
	const language = 'ja';
	const region = 'JP';
</script>

<APIProvider {apiKey} {language} {region}>
	<!-- Your map components here -->
</APIProvider>
svelte

Cloud-Based Map Styling with Map IDs

If you're using cloud-based map styling, you can specify map IDs:

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

	const apiKey = 'YOUR_API_KEY';
	const mapIds = ['YOUR_MAP_ID_1', 'YOUR_MAP_ID_2'];

	const mapOptions = {
		center: { lat: 35.6812362, lng: 139.7649361 },
		zoom: 10,
		mapId: 'YOUR_MAP_ID_1'
	};
</script>

<APIProvider {apiKey} {mapIds}>
	<GoogleMap id="map" options={mapOptions} />
</APIProvider>
svelte

Notes

  • The APIProvider component must be the parent of all other Google Maps components.
  • It dynamically loads the Google Maps JavaScript API with the specified configuration.
  • Child components will not render until the API is fully loaded.
  • You only need one APIProvider component per application, even if you have multiple maps.