Stay organized with collectionsSave and categorize content based on your preferences.
This example demonstrates callingfetchAutocompleteSuggestions()for the input
"Tadi", then callingtoPlace()on the first prediction result, followed by a
call tofetchFields()to get place details.
/*** Demonstrates making a single request for Place predictions, then requests Place Details for the first result.*/asyncfunctioninit(){// @ts-ignoreconst{Place,AutocompleteSessionToken,AutocompleteSuggestion}=awaitgoogle.maps.importLibrary("places")asgoogle.maps.PlacesLibrary;// Add an initial request body.letrequest={input:"Tadi",locationRestriction:{west:-122.44,north:37.8,east:-122.39,south:37.78},origin:{lat:37.7893,lng:-122.4039},includedPrimaryTypes:["restaurant"],language:"en-US",region:"us",};// Create a session token.consttoken=newAutocompleteSessionToken();// Add the token to the request.// @ts-ignorerequest.sessionToken=token;// Fetch autocomplete suggestions.const{suggestions}=awaitAutocompleteSuggestion.fetchAutocompleteSuggestions(request);consttitle=document.getElementById('title')asHTMLElement;title.appendChild(document.createTextNode('Query predictions for "'+request.input+'":'));for(letsuggestionofsuggestions){constplacePrediction=suggestion.placePrediction;// Create a new list element.constlistItem=document.createElement('li');constresultsElement=document.getElementById("results")asHTMLElement;listItem.appendChild(document.createTextNode(placePrediction.text.toString()));resultsElement.appendChild(listItem);}letplace=suggestions[0].placePrediction.toPlace();// Get first predicted place.awaitplace.fetchFields({fields:['displayName','formattedAddress'],});constplaceInfo=document.getElementById("prediction")asHTMLElement;placeInfo.textContent='First predicted place: '+place.displayName+': '+place.formattedAddress;}init();
/*** Demonstrates making a single request for Place predictions, then requests Place Details for the first result.*/asyncfunctioninit(){// @ts-ignoreconst{Place,AutocompleteSessionToken,AutocompleteSuggestion}=awaitgoogle.maps.importLibrary("places");// Add an initial request body.letrequest={input:"Tadi",locationRestriction:{west:-122.44,north:37.8,east:-122.39,south:37.78,},origin:{lat:37.7893,lng:-122.4039},includedPrimaryTypes:["restaurant"],language:"en-US",region:"us",};// Create a session token.consttoken=newAutocompleteSessionToken();// Add the token to the request.// @ts-ignorerequest.sessionToken=token;// Fetch autocomplete suggestions.const{suggestions}=awaitAutocompleteSuggestion.fetchAutocompleteSuggestions(request);consttitle=document.getElementById("title");title.appendChild(document.createTextNode('Query predictions for "'+request.input+'":'),);for(letsuggestionofsuggestions){constplacePrediction=suggestion.placePrediction;// Create a new list element.constlistItem=document.createElement("li");constresultsElement=document.getElementById("results");listItem.appendChild(document.createTextNode(placePrediction.text.toString()),);resultsElement.appendChild(listItem);}letplace=suggestions[0].placePrediction.toPlace();// Get first predicted place.awaitplace.fetchFields({fields:["displayName","formattedAddress"],});constplaceInfo=document.getElementById("prediction");placeInfo.textContent="First predicted place: "+place.displayName+": "+place.formattedAddress;}init();
/** Always set the map height explicitly to define the size of the div element* that contains the map.*/#map{height:100%;}/** Optional: Makes the sample page fill the window.*/html,body{height:100%;margin:0;padding:0;}
Git and Node.js are required to run this sample locally. Follow theseinstructionsto install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis example shows how to use the Place Autocomplete Data API to get place predictions for a given input and retrieve place details for the first prediction.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code demonstrates fetching autocomplete suggestions for the input "Tadi", restricting the search to restaurants in a specific area.\u003c/p\u003e\n"],["\u003cp\u003eIt then uses \u003ccode\u003etoPlace()\u003c/code\u003e to convert the first suggestion into a Place object and calls \u003ccode\u003efetchFields()\u003c/code\u003e to get details like name and address.\u003c/p\u003e\n"],["\u003cp\u003eThe example provides code snippets in both TypeScript and JavaScript, along with HTML and CSS for displaying the results.\u003c/p\u003e\n"]]],[],null,["This example demonstrates calling `fetchAutocompleteSuggestions()` for the input\n\"Tadi\", then calling `toPlace()` on the first prediction result, followed by a\ncall to `fetchFields()` to get place details.\n\nRead the\n[documentation](/maps/documentation/javascript/place-autocomplete-data). \n\nTypeScript \n\n```typescript\n/**\n * Demonstrates making a single request for Place predictions, then requests Place Details for the first result.\n */\nasync function init() {\n // @ts-ignore\n const { Place, AutocompleteSessionToken, AutocompleteSuggestion } = await google.maps.importLibrary(\"places\") as google.maps.PlacesLibrary;\n\n // Add an initial request body.\n let request = {\n input: \"Tadi\",\n locationRestriction: { west: -122.44, north: 37.8, east: -122.39, south: 37.78 },\n origin: { lat: 37.7893, lng: -122.4039 },\n includedPrimaryTypes: [\"restaurant\"],\n language: \"en-US\",\n region: \"us\",\n };\n\n // Create a session token.\n const token = new AutocompleteSessionToken();\n // Add the token to the request.\n // @ts-ignore\n request.sessionToken = token;\n // Fetch autocomplete suggestions.\n const { suggestions } = await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);\n\n const title = document.getElementById('title') as HTMLElement;\n title.appendChild(document.createTextNode('Query predictions for \"' + request.input + '\":'));\n\n for (let suggestion of suggestions) {\n const placePrediction = suggestion.placePrediction;\n\n // Create a new list element.\n const listItem = document.createElement('li');\n const resultsElement = document.getElementById(\"results\") as HTMLElement;\n listItem.appendChild(document.createTextNode(placePrediction.text.toString()));\n resultsElement.appendChild(listItem);\n }\n\n let place = suggestions[0].placePrediction.toPlace(); // Get first predicted place.\n await place.fetchFields({\n fields: ['displayName', 'formattedAddress'],\n });\n\n const placeInfo = document.getElementById(\"prediction\") as HTMLElement;\n placeInfo.textContent = 'First predicted place: ' + place.displayName + ': ' + place.formattedAddress;\n\n}\n\ninit();https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/place-autocomplete-data-simple/index.ts#L8-L68\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\n/**\n * Demonstrates making a single request for Place predictions, then requests Place Details for the first result.\n */\nasync function init() {\n // @ts-ignore\n const { Place, AutocompleteSessionToken, AutocompleteSuggestion } =\n await google.maps.importLibrary(\"places\");\n // Add an initial request body.\n let request = {\n input: \"Tadi\",\n locationRestriction: {\n west: -122.44,\n north: 37.8,\n east: -122.39,\n south: 37.78,\n },\n origin: { lat: 37.7893, lng: -122.4039 },\n includedPrimaryTypes: [\"restaurant\"],\n language: \"en-US\",\n region: \"us\",\n };\n // Create a session token.\n const token = new AutocompleteSessionToken();\n\n // Add the token to the request.\n // @ts-ignore\n request.sessionToken = token;\n\n // Fetch autocomplete suggestions.\n const { suggestions } =\n await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);\n const title = document.getElementById(\"title\");\n\n title.appendChild(\n document.createTextNode('Query predictions for \"' + request.input + '\":'),\n );\n\n for (let suggestion of suggestions) {\n const placePrediction = suggestion.placePrediction;\n // Create a new list element.\n const listItem = document.createElement(\"li\");\n const resultsElement = document.getElementById(\"results\");\n\n listItem.appendChild(\n document.createTextNode(placePrediction.text.toString()),\n );\n resultsElement.appendChild(listItem);\n }\n\n let place = suggestions[0].placePrediction.toPlace(); // Get first predicted place.\n\n await place.fetchFields({\n fields: [\"displayName\", \"formattedAddress\"],\n });\n\n const placeInfo = document.getElementById(\"prediction\");\n\n placeInfo.textContent =\n \"First predicted place: \" +\n place.displayName +\n \": \" +\n place.formattedAddress;\n}\n\ninit();https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/place-autocomplete-data-simple/docs/index.js#L7-L83\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\n/* \n * Always set the map height explicitly to define the size of the div element\n * that contains the map. \n */\n#map {\n height: 100%;\n}\n\n/* \n * Optional: Makes the sample page fill the window. \n */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/place-autocomplete-data-simple/docs/style.css#L7-L24\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003ePlace Autocomplete Data API Predictions\u003c/title\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" /\u003e\n \u003cscript type=\"module\" src=\"./index.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv id=\"title\"\u003e\u003c/div\u003e\n \u003cul id=\"results\"\u003e\u003c/ul\u003e\n \u003cp\u003e\u003cspan id=\"prediction\"\u003e\u003c/span\u003e\u003c/p\u003e\n \u003cimg\n class=\"powered-by-google\"\n src=\"https://storage.googleapis.com/geo-devrel-public-buckets/powered_by_google_on_white.png\"\n alt=\"Powered by Google\"\n /\u003e\n\n \u003c!-- prettier-ignore --\u003e\n \u003cscript\u003e(g=\u003e{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=()=\u003eh||(h=new Promise(async(f,n)=\u003e{await (a=m.createElement(\"script\"));e.set(\"libraries\",[...r]+\"\");for(k in g)e.set(k.replace(/[A-Z]/g,t=\u003e\"_\"+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=()=\u003eh=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)=\u003er.add(f)&&u().then(()=\u003ed[l](f,...n))})\n ({key: \"AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg\", v: \"weekly\"});\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/place-autocomplete-data-simple/docs/index.html#L8-L31\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/place-autocomplete-data-simple/jsfiddle) [Google Cloud Shell](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fjs-samples&cloudshell_git_branch=sample-place-autocomplete-data-simple&cloudshell_tutorial=cloud_shell_instructions.md&cloudshell_workspace=.)\n\nClone Sample\n\n\nGit and Node.js are required to run this sample locally. Follow these [instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to install Node.js and NPM. The following commands clone, install dependencies and start the sample application. \n\n git clone -b sample-place-autocomplete-data-simple https://github.com/googlemaps/js-samples.git\n cd js-samples\n npm i\n npm start\n\n\nOther samples can be tried by switching to any branch beginning with `sample-`\u003cvar translate=\"no\"\u003eSAMPLE_NAME\u003c/var\u003e. \n\n git checkout sample-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eSAMPLE_NAME\u003c/span\u003e\u003c/var\u003e\n npm i\n npm start"]]