API Reference

Complete guide to configuration parameters, methods, and options for Places Autocomplete JS.

Quick Example

Here's a complete example showing all major configuration options:

const autocomplete = new PlacesAutocomplete({
  containerId: 'autocomplete-container',
  googleMapsApiKey: 'YOUR_GOOGLE_MAPS_API_KEY',
  googleMapsApiVersion: 'weekly',
  fetchFields: ['formattedAddress', 'addressComponents'],
  onResponse: (placeDetails) => {
    console.log('Selected place:', placeDetails);
  },
  onError: (error) => {
    console.error('Error:', error.message);
  },
  options: {
    placeholder: 'Enter your address...',
    debounce: 150,
    autofocus: true
  },
  requestParams: {
    language: 'en-GB',
    region: 'GB',
    includedRegionCodes: ['GB']
  }
});
					
				

Configuration Parameters

The main configuration object passed to the PlacesAutocomplete constructor.

ParameterTypeRequiredDescription
containerIdstringYesThe ID of the HTML element where the autocomplete widget will be rendered.
googleMapsApiKeystringYesYour Google Maps API Key with the Places API (New) enabled.
googleMapsApiVersionstringNoAPI version to load ("weekly", "quarterly", "beta"). Default: "weekly"
onResponsefunctionNoCallback triggered with selected place details. Receives a Place object.
onErrorfunctionNoCallback triggered on errors. Receives an Error object or string.
optionsobjectNoUI behavior and appearance options. See UI Options below.
requestParamsobjectNoGoogle Places API request parameters. See Request Parameters below.
fetchFieldsarrayNoPlace Data Fields to fetch. Default: ['formattedAddress', 'addressComponents']

UI & Behaviour Options

Options to customise the component's appearance and behaviour, passed via the options parameter.

OptionTypeDefaultDescription
placeholderstring"Start typing your address ..."Placeholder text for the input field.
debouncenumber100Delay in milliseconds before triggering an API request after user stops typing. Set to 0 to disable.
distancebooleanfalseWhether to attempt to show distance in suggestions (requires origin in requestParams).
distance_units'km' | 'miles''km'Units to display distance in if distance is true.
labelstring""Optional label text displayed above the input field.
autofocusbooleanfalseIf true, automatically focuses the input field on initialisation.
autocompletestring'off'Standard HTML autocomplete attribute for the input field.
classesobject(See default classes below)Object to override default CSS classes for styling. See "Styling" section.
clear_inputbooleanfalseIf true, clears the input field after a suggestion is selected. If false (default), the input field retains the formattedAddress of the selected place.
response_type'json' | 'place''json'Return format: 'json' for plain JSON object (default), 'place' for full Google Maps Place instance with method access.
show_place_typebooleanfalseWhether to display icons representing the place type (🏪, 🍽️, etc.). Mutually exclusive with distance.

API Request Parameters

Parameters sent to the Google Places Autocomplete API via the requestParams option. See Google's AutocompleteRequest documentation for all available options.

ParameterTypeDescription
languagestringLanguage code for results (e.g., "en", "fr", "de").
regionstringRegion code as ccTLD (e.g., "GB", "US").
includedRegionCodesstring[]Array of up to 5 CLDR region codes to restrict results.
locationBiasobjectArea to bias results towards (circle or rectangle).
locationRestrictionobjectArea to strictly restrict results to.
originLatLngLiteralOrigin point for distance calculations { lat, lng }.

Examples

// Basic region filtering
requestParams: {
  language: 'en-GB',
  region: 'GB',
  includedRegionCodes: ['GB']
}

// With location biasing
requestParams: {
  language: 'en-US',
  region: 'US',
  locationBias: {
    lat: 53.39670653190076,
    lng: -2.9346197562747967
  }
}

// With origin for distance calculations
requestParams: {
  language: 'fr',
  region: 'FR',
  includedRegionCodes: ['FR'],
  origin: { lat: 48.8566, lng: 2.3522 } // Paris
}

// Strict location restriction
requestParams: {
    locationRestriction: {
        north: 54.206681559752205,
        east: -0.7812994437747967,
        south: 52.95447972935652,
        west: -3.6816900687747967
    }
}
					
				

Fetch Fields

Specify which place data fields to retrieve when a user selects a suggestion. This directly impacts API costs.

Billing Impact

Different fields have different costs. See Google's Place Data Fields pricing for details.

Common Fields

  • formattedAddress - The complete address string
  • addressComponents - Structured address parts (street, city, etc.)
  • location - Latitude/longitude coordinates
  • displayName - Human-readable place name
  • types - Array of place types
  • viewport - Recommended map viewport
  • plusCode - Plus code representation

Examples

// Minimal fields (lowest cost)
const autocomplete = new PlacesAutocomplete({
  containerId: 'autocomplete-container',
  googleMapsApiKey: 'YOUR_API_KEY',
  fetchFields: ['formattedAddress']
});

// Standard fields
fetchFields: ['formattedAddress', 'addressComponents', 'location']

// Extensive fields (higher cost)
fetchFields: [
  'formattedAddress',
  'addressComponents',
  'location',
  'displayName',
  'types',
  'viewport',
  'plusCode'
]
					
				

Public Methods

The PlacesAutocomplete instance provides several methods for dynamic control.

clear()

Clears the input field, removes visible suggestions, and refreshes the session token.

autocomplete.clear();

destroy()

Removes event listeners and cleans up DOM elements. Use when the component is no longer needed.

autocomplete.destroy();

focus()

Sets focus on the autocomplete text input field.

autocomplete.focus();

setInputValue(lat, lng)

Sets input by reverse geocoding coordinates. Requires Geocoding API.

await autocomplete.setInputValue(48.8584, 2.2945);

setFetchFields(fields)

Dynamically updates the Place Data Fields to fetch. Merged with default fields.

autocomplete.setFetchFields(['displayName', 'types', 'location']);

getFetchFields()

Returns the current array of Place Data Fields that will be requested.

const fields = autocomplete.getFetchFields();

setRequestParams(params)

Dynamically updates API request parameters. Useful for changing search criteria after initialisation.

autocomplete.setRequestParams({ region: "US", language: "en-US" });

getRequestParams()

Returns the current API request parameters.

const params = autocomplete.getRequestParams();

setOptions(options)

Dynamically updates UI behaviour and appearance options.

autocomplete.setOptions({ placeholder: "New placeholder...", debounce: 250 });

getOptions()

Returns the current UI and behaviour options.

const options = autocomplete.getOptions();

Complete Example

// Clear input and reset
autocomplete.clear();

// Update request parameters dynamically
autocomplete.setRequestParams({
  region: 'US',
  language: 'en-US',
  origin: { lat: 40.7128, lng: -74.0060 }
});

// Update UI options
autocomplete.setOptions({
  placeholder: 'New placeholder...',
  debounce: 200
});

// Update fetch fields
autocomplete.setFetchFields(['displayName', 'types', 'location']);

// Get current configuration
const currentParams = autocomplete.getRequestParams();
const currentOptions = autocomplete.getOptions();
const currentFields = autocomplete.getFetchFields();

// Clean up when done
autocomplete.destroy();