place-autocomplete-data-session
Sample code supporting Google Maps Platform JavaScript API documentation.
author(s):
Geo Developer IX Documentation Team
HTML
<!doctype html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Place Autocomplete Data API Session</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script>
// prettier-ignore
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "GOOGLE_MAPS_API_KEY"
});
</script>
</head>
<body>
<gmp-map center="37.7893, -122.4039" zoom="12" map-id="DEMO_MAP_ID">
<div class="controls" slot="control-inline-start-block-start">
<input
type="text"
class="input"
placeholder="Search for a place..."
autocomplete="off" /><!-- Turn off the input's own autocomplete (not supported by all browsers).-->
<div class="token-status"></div>
<div class="title"></div>
<ol class="results"></ol>
</div>
</gmp-map>
</body>
</html>
CSS
/**
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
gmp-map {
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.place-button {
height: 3rem;
width: 100%;
background-color: transparent;
text-align: left;
border: none;
cursor: pointer;
}
.place-button:focus-visible {
outline: 2px solid #0056b3;
border-radius: 2px;
}
.input {
width: 300px;
font-size: small;
margin-bottom: 1rem;
}
/* Styles for the floating panel */
.controls {
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
font-family: sans-serif;
font-size: small;
margin: 12px;
padding: 1rem;
}
.title {
font-weight: bold;
margin-top: 1rem;
margin-bottom: 0.5rem;
}
.results {
list-style-type: none;
margin: 0;
padding: 0;
}
.results li:not(:last-child) {
border-bottom: 1px solid #ddd;
}
.results li:hover {
background-color: #eee;
}
JavaScript
'use strict';
/**
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
const mapElement = document.querySelector('gmp-map');
let innerMap;
let marker;
const titleElement = document.querySelector('.title');
const resultsContainerElement = document.querySelector('.results');
const inputElement = document.querySelector('input');
const tokenStatusElement = document.querySelector('.token-status');
let newestRequestId = 0;
let tokenCount = 0;
// Create an initial request body.
const request = {
input: '',
includedPrimaryTypes: [
'restaurant',
'cafe',
'museum',
'park',
'botanical_garden',
],
};
async function init() {
await google.maps.importLibrary('maps');
innerMap = mapElement.innerMap;
innerMap.setOptions({
mapTypeControl: false,
});
// Update request center and bounds when the map bounds change.
innerMap.addListener('bounds_changed', () => {
request.locationRestriction = innerMap.getBounds();
request.origin = innerMap.getCenter();
});
inputElement.addEventListener('input', makeAutocompleteRequest);
}
async function makeAutocompleteRequest(inputEvent) {
// To avoid race conditions, store the request ID and compare after the request.
const requestId = ++newestRequestId;
const { AutocompleteSuggestion } =
await google.maps.importLibrary('places');
if (!inputEvent.target?.value) {
titleElement.textContent = '';
resultsContainerElement.replaceChildren();
return;
}
// Add the latest char sequence to the request.
request.input = inputEvent.target.value;
// Fetch autocomplete suggestions and show them in a list.
const { suggestions } =
await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);
// If the request has been superseded by a newer request, do not render the output.
if (requestId !== newestRequestId)...