PlaceAutocomplete

Storybook

Open the PlaceAutocomplete story

PlaceAutocomplete renders Google's PlaceAutocompleteElement (Places API (New)): a search box with place suggestions. When the user picks a suggestion, the selected place is fetched with the fields you ask for and passed to onPlaceSelect.

Props

PropTypeDefaultDescription
fieldsstring[]['displayName', 'formattedAddress', 'location', 'viewport']Place fields fetched with place.fetchFields() after a selection. Pass [] to skip the request and receive the bare Place.
placeholderstringInput placeholder.
includedRegionCodesstring[]Up to 15 CLDR region codes, such as ['jp'].
includedPrimaryTypesstring[]Up to 5 primary place types, such as ['restaurant'].
locationBiasgoogle.maps.places.LocationBiasSoft area to prefer results from.
locationRestrictiongoogle.maps.places.LocationRestrictionArea results must come from.
requestedLanguagestringLanguage of the results.
requestedRegionstringRegion used to format results.
namestringname of the input, for forms.
disabledbooleanDisables the input.
optionsgoogle.maps.places.PlaceAutocompleteElementOptionsOther element options. Individual props override these.
idstringid of the wrapper element.
classstring''Class of the wrapper element.
stylestringInline style of the wrapper element.
elementgoogle.maps.places.PlaceAutocompleteElement | nullnullThe element. Bindable: read it with bind:element.

Event Props

PropTypeDescription
onPlaceSelect(place: google.maps.places.Place) => voidCalled with the selected place after its fields are fetched.
onError(error: Error) => voidCalled when suggestions or place details fail to load. Without it, errors are logged to the console.

Usage

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

	let map = $state<google.maps.Map | null>(null);
	let place = $state<google.maps.places.Place | null>(null);

	function handlePlaceSelect(selected: google.maps.places.Place) {
		place = selected;
		if (selected.viewport) {
			map?.fitBounds(selected.viewport);
		} else if (selected.location) {
			map?.setCenter(selected.location);
		}
	}
</script>

<APIProvider apiKey="YOUR_API_KEY">
	<PlaceAutocomplete
		placeholder="Search for a place"
		includedRegionCodes={['jp']}
		onPlaceSelect={handlePlaceSelect}
	/>

	<div style="height: 400px;">
		<GoogleMap bind:map center={{ lat: 35.6812, lng: 139.7671 }} zoom={12} mapId="DEMO_MAP_ID">
			{#if place?.location}
				<AdvancedMarker position={place.location} title={place.displayName ?? ''} />
			{/if}
		</GoogleMap>
	</div>
</APIProvider>

{#if place}
	<p>{place.displayName}: {place.formattedAddress}</p>
{/if}
svelte

Migrating from Autocomplete / StandaloneSearchBox

Autocomplete and StandaloneSearchBox wrapped google.maps.places.Autocomplete and SearchBox, which are not available to new Google Cloud customers since March 2025. They were removed. Replace them with PlaceAutocomplete:

  • onPlaceChanged + getPlace() becomes onPlaceSelect(place). The place is a google.maps.places.Place, so field names are camelCase (displayName, formattedAddress, location) instead of name, formatted_address, geometry.location.
  • options.fields becomes fields, using the new field names.
  • options.componentRestrictions.country becomes includedRegionCodes; options.bounds becomes locationBias or locationRestriction; options.types becomes includedPrimaryTypes.
  • The input element is created by Google. Style it with CSS on the element (see Google's styling guide) rather than passing your own <input>.

Notes

  • Enable Places API (New) for your API key. The legacy Places API is not enough.
  • PlaceAutocomplete does not need a GoogleMap; it only needs to be inside APIProvider.
  • Billing: each selection starts a session that ends with the fetchFields() call. Request only the fields you use.