// This example retrieves autocomplete predictions programmatically from the// autocomplete service, and displays them as an HTML list.// This example requires the Places library. Include the libraries=places// parameter when you first load the API. For example:// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">functioninitService():void{constdisplaySuggestions=function(predictions:google.maps.places.QueryAutocompletePrediction[]|null,status:google.maps.places.PlacesServiceStatus){if(status!=google.maps.places.PlacesServiceStatus.OK||!predictions){alert(status);return;}predictions.forEach((prediction)=>{constli=document.createElement("li");li.appendChild(document.createTextNode(prediction.description));(document.getElementById("results")asHTMLUListElement).appendChild(li);});};constservice=newgoogle.maps.places.AutocompleteService();service.getQueryPredictions({input:"pizza near Syd"},displaySuggestions);}declareglobal{interfaceWindow{initService:()=>void;}}window.initService=initService;
// This example retrieves autocomplete predictions programmatically from the// autocomplete service, and displays them as an HTML list.// This example requires the Places library. Include the libraries=places// parameter when you first load the API. For example:// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">functioninitService(){constdisplaySuggestions=function(predictions,status){if(status!=google.maps.places.PlacesServiceStatus.OK||!predictions){alert(status);return;}predictions.forEach((prediction)=>{constli=document.createElement("li");li.appendChild(document.createTextNode(prediction.description));document.getElementById("results").appendChild(li);});};constservice=newgoogle.maps.places.AutocompleteService();service.getQueryPredictions({input:"pizza near Syd"},displaySuggestions);}window.initService=initService;
<html> <head> <title>Retrieving Autocomplete Predictions</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <p>Query suggestions for 'pizza near Syd':</p> <ul id="results"></ul> <!-- Replace Powered By Google image src with self hosted image. https://developers.google.com/maps/documentation/places/web-service/policies#other_attribution_requirements --> <img class="powered-by-google" src="https://storage.googleapis.com/geo-devrel-public-buckets/powered_by_google_on_white.png" alt="Powered by Google" /> <!-- The `defer` attribute causes the script to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initService&libraries=places&v=weekly" defer ></script> </body> </html>
Git and Node.js are required to run this sample locally. Follow these instructions to 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-08-20 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to use the Google Maps Places Autocomplete Service to predict places based on a partial query.\u003c/p\u003e\n"],["\u003cp\u003eIt fetches query predictions for the input "pizza near Syd" and displays them in an HTML list.\u003c/p\u003e\n"],["\u003cp\u003eThe sample utilizes both TypeScript and JavaScript implementations, showcasing how to retrieve and display predictions programmatically.\u003c/p\u003e\n"],["\u003cp\u003eYou can interact with the sample using provided links to JSFiddle.net and Google Cloud Shell, or clone and run it locally following instructions for Git and Node.js.\u003c/p\u003e\n"]]],["The code uses the Google Maps Places API to predict queries. It initializes an `AutocompleteService`, then requests predictions for \"pizza near Syd\". Upon receiving a response, the `displaySuggestions` function creates an HTML list (`\u003cul\u003e`) with the ID \"results\". Each prediction's description is added as a list item (`\u003cli\u003e`) to this list, which is then displayed on the webpage. This requires the `places` library.\n"],null,["This example executes a query prediction request for the phrase \"pizza near Syd\"\nand displays the results as an HTML list.\n\nRead the\n[documentation](/maps/documentation/javascript/places-autocomplete#place_autocomplete_service). \n\nTypeScript \n\n```typescript\n// This example retrieves autocomplete predictions programmatically from the\n// autocomplete service, and displays them as an HTML list.\n// This example requires the Places library. Include the libraries=places\n// parameter when you first load the API. For example:\n// \u003cscript src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places\"\u003e\nfunction initService(): void {\n const displaySuggestions = function (\n predictions: google.maps.places.QueryAutocompletePrediction[] | null,\n status: google.maps.places.PlacesServiceStatus\n ) {\n if (status != google.maps.places.PlacesServiceStatus.OK || !predictions) {\n alert(status);\n return;\n }\n\n predictions.forEach((prediction) =\u003e {\n const li = document.createElement(\"li\");\n\n li.appendChild(document.createTextNode(prediction.description));\n (document.getElementById(\"results\") as HTMLUListElement).appendChild(li);\n });\n };\n\n const service = new google.maps.places.AutocompleteService();\n\n service.getQueryPredictions({ input: \"pizza near Syd\" }, displaySuggestions);\n}\n\ndeclare global {\n interface Window {\n initService: () =\u003e void;\n }\n}\nwindow.initService = initService;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/places-queryprediction/index.ts#L8-L41\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\n// This example retrieves autocomplete predictions programmatically from the\n// autocomplete service, and displays them as an HTML list.\n// This example requires the Places library. Include the libraries=places\n// parameter when you first load the API. For example:\n// \u003cscript src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places\"\u003e\nfunction initService() {\n const displaySuggestions = function (predictions, status) {\n if (status != google.maps.places.PlacesServiceStatus.OK || !predictions) {\n alert(status);\n return;\n }\n\n predictions.forEach((prediction) =\u003e {\n const li = document.createElement(\"li\");\n\n li.appendChild(document.createTextNode(prediction.description));\n document.getElementById(\"results\").appendChild(li);\n });\n };\n\n const service = new google.maps.places.AutocompleteService();\n\n service.getQueryPredictions({ input: \"pizza near Syd\" }, displaySuggestions);\n}\n\nwindow.initService = initService;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-queryprediction/docs/index.js#L7-L32\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-queryprediction/docs/style.css#L7-L6\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eRetrieving Autocomplete 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 \u003cp\u003eQuery suggestions for 'pizza near Syd':\u003c/p\u003e\n \u003cul id=\"results\"\u003e\u003c/ul\u003e\n \u003c!-- Replace Powered By Google image src with self hosted image. https://developers.google.com/maps/documentation/places/web-service/policies#other_attribution_requirements --\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!-- \n The `defer` attribute causes the script to execute after the full HTML\n document has been parsed. For non-blocking uses, avoiding race conditions,\n and consistent behavior across browsers, consider loading using Promises. See\n https://developers.google.com/maps/documentation/javascript/load-maps-js-api\n for more information.\n --\u003e\n \u003cscript\n src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initService&libraries=places&v=weekly\"\n defer\n \u003e\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-queryprediction/docs/index.html#L8-L37\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/places-queryprediction/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-places-queryprediction&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-places-queryprediction 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"]]