letmap:google.maps.Map;letmarker:google.maps.marker.AdvancedMarkerElement;letinfoWindow:google.maps.InfoWindow;letcenter={lat:40.749933,lng:-73.98633};// New York CityasyncfunctioninitMap():Promise<void>{// Request needed libraries.//@ts-ignoreconst[{Map},{AdvancedMarkerElement}]=awaitPromise.all([google.maps.importLibrary("marker"),google.maps.importLibrary("places")]);// Initialize the map.map=newgoogle.maps.Map(document.getElementById('map')asHTMLElement,{center,zoom:13,mapId:'4504f8b37365c3d0',mapTypeControl:false,});//@ts-ignoreconstplaceAutocomplete=newgoogle.maps.places.PlaceAutocompleteElement();//@ts-ignoreplaceAutocomplete.id='place-autocomplete-input';placeAutocomplete.locationBias=center;constcard=document.getElementById('place-autocomplete-card')asHTMLElement;//@ts-ignorecard.appendChild(placeAutocomplete);map.controls[google.maps.ControlPosition.TOP_LEFT].push(card);// Create the marker and infowindow.marker=newgoogle.maps.marker.AdvancedMarkerElement({map,});infoWindow=newgoogle.maps.InfoWindow({});// Add the gmp-placeselect listener, and display the results on the map.//@ts-ignoreplaceAutocomplete.addEventListener('gmp-select',async({placePrediction})=>{constplace=placePrediction.toPlace();awaitplace.fetchFields({fields:['displayName','formattedAddress','location']});// If the place has a geometry, then present it on a map.if(place.viewport){map.fitBounds(place.viewport);}else{map.setCenter(place.location);map.setZoom(17);}letcontent='<div id="infowindow-content">'+'<span id="place-displayname" class="title">'+place.displayName+'</span><br />'+'<span id="place-address">'+place.formattedAddress+'</span>'+'</div>';updateInfoWindow(content,place.location);marker.position=place.location;});}// Helper function to create an info window.functionupdateInfoWindow(content,center){infoWindow.setContent(content);infoWindow.setPosition(center);infoWindow.open({map,anchor:marker,shouldFocus:false,});}initMap();
letmap;letmarker;letinfoWindow;letcenter={lat:40.749933,lng:-73.98633};// New York CityasyncfunctioninitMap(){// Request needed libraries.//@ts-ignoreconst[{Map},{AdvancedMarkerElement}]=awaitPromise.all([google.maps.importLibrary("marker"),google.maps.importLibrary("places")]);// Initialize the map.map=newgoogle.maps.Map(document.getElementById('map'),{center,zoom:13,mapId:'4504f8b37365c3d0',mapTypeControl:false,});//@ts-ignoreconstplaceAutocomplete=newgoogle.maps.places.PlaceAutocompleteElement();//@ts-ignoreplaceAutocomplete.id='place-autocomplete-input';placeAutocomplete.locationBias=center;constcard=document.getElementById('place-autocomplete-card');//@ts-ignorecard.appendChild(placeAutocomplete);map.controls[google.maps.ControlPosition.TOP_LEFT].push(card);// Create the marker and infowindow.marker=newgoogle.maps.marker.AdvancedMarkerElement({map,});infoWindow=newgoogle.maps.InfoWindow({});// Add the gmp-placeselect listener, and display the results on the map.//@ts-ignoreplaceAutocomplete.addEventListener('gmp-select',async({placePrediction})=>{constplace=placePrediction.toPlace();awaitplace.fetchFields({fields:['displayName','formattedAddress','location']});// If the place has a geometry, then present it on a map.if(place.viewport){map.fitBounds(place.viewport);}else{map.setCenter(place.location);map.setZoom(17);}letcontent='<div id="infowindow-content">'+'<span id="place-displayname" class="title">'+place.displayName+'</span><br />'+'<span id="place-address">'+place.formattedAddress+'</span>'+'</div>';updateInfoWindow(content,place.location);marker.position=place.location;});}// Helper function to create an info window.functionupdateInfoWindow(content,center){infoWindow.setContent(content);infoWindow.setPosition(center);infoWindow.open({map,anchor:marker,shouldFocus:false,});}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;}#place-autocomplete-card{background-color:#fff;border-radius:5px;box-shadow:rgba(0,0,0,0.35)0px5px15px;margin:10px;padding:5px;font-family:Roboto,sans-serif;font-size:large;font-weight:bold;}gmp-place-autocomplete{width:300px;}#infowindow-content.title{font-weight:bold;}#map#infowindow-content{display:inline;}
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 example demonstrates how to integrate an Autocomplete widget into a Google Map, allowing users to search for and select locations.\u003c/p\u003e\n"],["\u003cp\u003eUpon selection, the map adjusts its view to center on the chosen location and displays an information window with details like name and address.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code is provided in both TypeScript and JavaScript, with CSS for styling the map and Autocomplete elements.\u003c/p\u003e\n"],["\u003cp\u003eThe example leverages the Google Maps JavaScript API and requires an API key for functionality.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can clone the sample code repository and run it locally using Git and Node.js.\u003c/p\u003e\n"]]],[],null,["This example shows you how to add an Autocomplete widget to a Google map.\n\nRead the [documentation](/maps/documentation/javascript/place-autocomplete-new). \n\nTypeScript \n\n```typescript\nlet map: google.maps.Map;\nlet marker: google.maps.marker.AdvancedMarkerElement;\nlet infoWindow: google.maps.InfoWindow;\nlet center = { lat: 40.749933, lng: -73.98633 }; // New York City\nasync function initMap(): Promise\u003cvoid\u003e {\n // Request needed libraries.\n //@ts-ignore\n const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([\n google.maps.importLibrary(\"marker\"),\n google.maps.importLibrary(\"places\")\n ]);\n\n // Initialize the map.\n map = new google.maps.Map(document.getElementById('map') as HTMLElement, {\n center,\n zoom: 13,\n mapId: '4504f8b37365c3d0',\n mapTypeControl: false,\n });\n //@ts-ignore\n const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement();\n //@ts-ignore\n placeAutocomplete.id = 'place-autocomplete-input';\n placeAutocomplete.locationBias = center;\n\n const card = document.getElementById('place-autocomplete-card') as HTMLElement;\n //@ts-ignore\n card.appendChild(placeAutocomplete);\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(card);\n\n // Create the marker and infowindow.\n marker = new google.maps.marker.AdvancedMarkerElement({\n map,\n });\n\n infoWindow = new google.maps.InfoWindow({});\n\n // Add the gmp-placeselect listener, and display the results on the map.\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\n // If the place has a geometry, then present it on a map.\n if (place.viewport) {\n map.fitBounds(place.viewport);\n } else {\n map.setCenter(place.location);\n map.setZoom(17);\n }\n\n let content = '\u003cdiv id=\"infowindow-content\"\u003e' +\n '\u003cspan id=\"place-displayname\" class=\"title\"\u003e' + place.displayName + '\u003c/span\u003e\u003cbr /\u003e' +\n '\u003cspan id=\"place-address\"\u003e' + place.formattedAddress + '\u003c/span\u003e' +\n '\u003c/div\u003e';\n\n updateInfoWindow(content, place.location);\n marker.position = place.location;\n });\n}\n\n// Helper function to create an info window.\nfunction updateInfoWindow(content, center) {\n infoWindow.setContent(content);\n infoWindow.setPosition(center);\n infoWindow.open({\n map,\n anchor: marker,\n shouldFocus: false,\n });\n}\n\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/29b1d5be457a450848aa288697792a57ab9e3754/samples/place-autocomplete-map/index.ts#L8-L84\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\nlet map;\nlet marker;\nlet infoWindow;\nlet center = { lat: 40.749933, lng: -73.98633 }; // New York City\nasync function initMap() {\n // Request needed libraries.\n //@ts-ignore\n const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([\n google.maps.importLibrary(\"marker\"),\n google.maps.importLibrary(\"places\")\n ]);\n // Initialize the map.\n map = new google.maps.Map(document.getElementById('map'), {\n center,\n zoom: 13,\n mapId: '4504f8b37365c3d0',\n mapTypeControl: false,\n });\n //@ts-ignore\n const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement();\n //@ts-ignore\n placeAutocomplete.id = 'place-autocomplete-input';\n placeAutocomplete.locationBias = center;\n const card = document.getElementById('place-autocomplete-card');\n //@ts-ignore\n card.appendChild(placeAutocomplete);\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(card);\n // Create the marker and infowindow.\n marker = new google.maps.marker.AdvancedMarkerElement({\n map,\n });\n infoWindow = new google.maps.InfoWindow({});\n // Add the gmp-placeselect listener, and display the results on the map.\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 // If the place has a geometry, then present it on a map.\n if (place.viewport) {\n map.fitBounds(place.viewport);\n }\n else {\n map.setCenter(place.location);\n map.setZoom(17);\n }\n let content = '\u003cdiv id=\"infowindow-content\"\u003e' +\n '\u003cspan id=\"place-displayname\" class=\"title\"\u003e' + place.displayName + '\u003c/span\u003e\u003cbr /\u003e' +\n '\u003cspan id=\"place-address\"\u003e' + place.formattedAddress + '\u003c/span\u003e' +\n '\u003c/div\u003e';\n updateInfoWindow(content, place.location);\n marker.position = place.location;\n });\n}\n// Helper function to create an info window.\nfunction updateInfoWindow(content, center) {\n infoWindow.setContent(content);\n infoWindow.setPosition(center);\n infoWindow.open({\n map,\n anchor: marker,\n shouldFocus: false,\n });\n}\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/29b1d5be457a450848aa288697792a57ab9e3754/dist/samples/place-autocomplete-map/docs/index.js#L8-L75\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\n#place-autocomplete-card {\n background-color: #fff;\n border-radius: 5px;\n box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;\n margin: 10px;\n padding: 5px;\n font-family: Roboto, sans-serif;\n font-size: large;\n font-weight: bold;\n}\n\ngmp-place-autocomplete {\n width: 300px;\n}\n\n#infowindow-content .title {\n font-weight: bold;\n}\n\n#map #infowindow-content {\n display: inline;\n}\nhttps://github.com/googlemaps-samples/js-api-samples/blob/29b1d5be457a450848aa288697792a57ab9e3754/dist/samples/place-autocomplete-map/docs/style.css#L7-L47\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003ePlace Autocomplete map\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 class=\"place-autocomplete-card\" id=\"place-autocomplete-card\"\u003e\n \u003cp\u003eSearch for a place here:\u003c/p\u003e\n \u003c/div\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\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/29b1d5be457a450848aa288697792a57ab9e3754/dist/samples/place-autocomplete-map/docs/index.html#L8-L25\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-map/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-map\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."]]