asyncfunctioninitMap():Promise<void>{// Request needed libraries.awaitgoogle.maps.importLibrary("places")asgoogle.maps.PlacesLibrary;// Create the input HTML element, and append it.//@ts-ignoreconstplaceAutocomplete=newgoogle.maps.places.PlaceAutocompleteElement();//@ts-ignoredocument.body.appendChild(placeAutocomplete);// Inject HTML UI.constselectedPlaceTitle=document.createElement('p');selectedPlaceTitle.textContent='';document.body.appendChild(selectedPlaceTitle);constselectedPlaceInfo=document.createElement('pre');selectedPlaceInfo.textContent='';document.body.appendChild(selectedPlaceInfo);// Add the gmp-placeselect listener, and display the results.//@ts-ignoreplaceAutocomplete.addEventListener('gmp-select',async({placePrediction})=>{constplace=placePrediction.toPlace();awaitplace.fetchFields({fields:['displayName','formattedAddress','location']});selectedPlaceTitle.textContent='Selected Place:';selectedPlaceInfo.textContent=JSON.stringify(place.toJSON(),/* replacer */null,/* space */2);});}initMap();
asyncfunctioninitMap(){// Request needed libraries.awaitgoogle.maps.importLibrary("places");// Create the input HTML element, and append it.//@ts-ignoreconstplaceAutocomplete=newgoogle.maps.places.PlaceAutocompleteElement();//@ts-ignoredocument.body.appendChild(placeAutocomplete);// Inject HTML UI.constselectedPlaceTitle=document.createElement('p');selectedPlaceTitle.textContent='';document.body.appendChild(selectedPlaceTitle);constselectedPlaceInfo=document.createElement('pre');selectedPlaceInfo.textContent='';document.body.appendChild(selectedPlaceInfo);// Add the gmp-placeselect listener, and display the results.//@ts-ignoreplaceAutocomplete.addEventListener('gmp-select',async({placePrediction})=>{constplace=placePrediction.toPlace();awaitplace.fetchFields({fields:['displayName','formattedAddress','location']});selectedPlaceTitle.textContent='Selected Place:';selectedPlaceInfo.textContent=JSON.stringify(place.toJSON(),/* replacer */null,/* space */2);});}initMap();
/* * 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;}p{font-family:Roboto,sans-serif;font-weight:bold;}
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 implements an autocomplete widget that allows users to search for places.\u003c/p\u003e\n"],["\u003cp\u003eUpon place selection, it displays details like name, address, and location.\u003c/p\u003e\n"],["\u003cp\u003eThe sample is provided in both TypeScript and JavaScript, with the JavaScript compiled from the TypeScript version.\u003c/p\u003e\n"],["\u003cp\u003eUsers can interact with a live demo through JSFiddle or Google Cloud Shell and clone the sample code for local execution.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers are encouraged to explore the Place Overview component for displaying comprehensive place information using a pre-built UI.\u003c/p\u003e\n"]]],[],null,["This example adds an Autocomplete widget to a web page, and displays the results\nfor each selected place.\n\nRead the [documentation](/maps/documentation/javascript/place-autocomplete-new). \n\nTypeScript \n\n```typescript\nasync function initMap(): Promise\u003cvoid\u003e {\n // Request needed libraries.\n await google.maps.importLibrary(\"places\") as google.maps.PlacesLibrary;\n // Create the input HTML element, and append it.\n //@ts-ignore\n const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement();\n //@ts-ignore\n document.body.appendChild(placeAutocomplete);\n\n // Inject HTML UI.\n const selectedPlaceTitle = document.createElement('p');\n selectedPlaceTitle.textContent = '';\n document.body.appendChild(selectedPlaceTitle);\n\n const selectedPlaceInfo = document.createElement('pre');\n selectedPlaceInfo.textContent = '';\n document.body.appendChild(selectedPlaceInfo);\n\n // Add the gmp-placeselect listener, and display the results.\n //@ts-ignore\n placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) =\u003e {\n const place = placePrediction.toPlace();\n await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });\n selectedPlaceTitle.textContent = 'Selected Place:';\n selectedPlaceInfo.textContent = JSON.stringify(\n place.toJSON(), /* replacer */ null, /* space */ 2);\n });\n}\n\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/eb299344a468a9a0aa2e81ca28e51bb5395341ee/samples/place-autocomplete-element/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\nasync function initMap() {\n // Request needed libraries.\n await google.maps.importLibrary(\"places\");\n // Create the input HTML element, and append it.\n //@ts-ignore\n const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement();\n //@ts-ignore\n document.body.appendChild(placeAutocomplete);\n // Inject HTML UI.\n const selectedPlaceTitle = document.createElement('p');\n selectedPlaceTitle.textContent = '';\n document.body.appendChild(selectedPlaceTitle);\n const selectedPlaceInfo = document.createElement('pre');\n selectedPlaceInfo.textContent = '';\n document.body.appendChild(selectedPlaceInfo);\n // Add the gmp-placeselect listener, and display the results.\n //@ts-ignore\n placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) =\u003e {\n const place = placePrediction.toPlace();\n await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });\n selectedPlaceTitle.textContent = 'Selected Place:';\n selectedPlaceInfo.textContent = JSON.stringify(place.toJSON(), /* replacer */ null, /* space */ 2);\n });\n}\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/eb299344a468a9a0aa2e81ca28e51bb5395341ee/dist/samples/place-autocomplete-element/docs/index.js#L8-L36\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}\n\np {\n font-family: Roboto, sans-serif;\n font-weight: bold;\n}\nhttps://github.com/googlemaps-samples/js-api-samples/blob/eb299344a468a9a0aa2e81ca28e51bb5395341ee/dist/samples/place-autocomplete-element/docs/style.css#L7-L29\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003ePlace Autocomplete element\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 style=\"font-family: roboto, sans-serif\"\u003eSearch for a place here:\u003c/p\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: \"AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8\", v: \"weekly\"});\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps-samples/js-api-samples/blob/eb299344a468a9a0aa2e81ca28e51bb5395341ee/dist/samples/place-autocomplete-element/docs/index.html#L8-L22\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/place-autocomplete-element/jsfiddle)\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 https://github.com/googlemaps-samples/js-api-samples.git\n cd samples/place-autocomplete-element\n npm i\n npm start\n\nUse the Place Overview component\n\nThe Place Overview component displays detailed information about millions of businesses,\nincluding opening hours, star reviews, and photos, plus directions and other\nactions in a premade UI in 5 sizes and formats. It is part of the\n[Extended Component Library](https://github.com/googlemaps/extended-component-library),\nfrom Google Maps Platform, a set of web components that helps developers build better maps\nand location features faster.\n\nUse the [Place Overview configurator](https://configure.mapsplatform.google/place-overview)\nto create embeddable code for a custom Place Overview component, then export\nit to be used with popular frameworks like React and Angular or no framework at all."]]