When building specialised applications like restaurant finders, hotel booking platforms, or
healthcare locators, you need to filter autocomplete results to specific place types. This
example demonstrates using includedPrimaryTypes to restrict results to
particular categories of establishments.
Select a place type from the dropdown below to filter the autocomplete suggestions. The search will only return places matching the selected category.
Establishments that serve prepared food
Google Maps provides an extensive list of place types. Here are some commonly used categories:
View the complete list of place types in the Google Maps documentation.
Type-specific filtering uses the Places API's type system to narrow down search results.
includedPrimaryTypes array to your requestParams. This tells the API to only return places matching these types.
You can specify multiple types if needed.requestParams and use a {#key} block to force component re-initialisation with the new type filter.types field in fetchFields to show what category the place belongs
to. This helps users understand the search results.locationRestriction and region for powerful, targeted searches.Here's a complete implementation of type-specific search. Customise the available types based on your application's requirements.
<script lang="ts">
import { PlaceAutocomplete } from 'places-autocomplete-svelte';
import type { PlaceResult } from 'places-autocomplete-svelte/interfaces';
let selectedType = $state('restaurant');
let componentKey = $state({});
// Configure the search to only return specific place types
let requestParams = $state({
language: 'en-gb',
region: 'GB',
includedPrimaryTypes: [selectedType] // Restrict to selected type
});
const options = {
placeholder: `Search for ${selectedType}s...`,
clear_input: false
};
const onResponse = (response: PlaceResult) => {
console.log('Selected place:', response);
// response.types will contain the place type information
};
// When type changes, update params and reset component
const handleTypeChange = () => {
requestParams = {
...requestParams,
includedPrimaryTypes: [selectedType]
};
componentKey = {}; // Force re-initialisation
};
</script>
<!-- Type selector -->
<select bind:value={selectedType} onchange={handleTypeChange}>
<option value="restaurant">Restaurants</option>
<option value="cafe">Cafés</option>
<option value="hotel">Hotels</option>
<option value="hospital">Hospitals</option>
<!-- Add more types as needed -->
</select>
<!-- Component with type filtering -->
{#key componentKey}
<PlaceAutocomplete
{onResponse}
{requestParams}
{options}
/>
{/key}