Stay organized with collections
Save and categorize content based on your preferences.
This example creates a map with a search box for users to enter geographical
searches. The search box returns a pick list that includes both places and
predicted search terms.
// This example adds a search box to a map, using the Google Place Autocomplete// feature. People can enter geographical searches. The search box will return a// pick list containing a mix of places and predicted search terms.// 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">functioninitAutocomplete(){constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{center:{lat:-33.8688,lng:151.2195},zoom:13,mapTypeId:"roadmap",});// Create the search box and link it to the UI element.constinput=document.getElementById("pac-input")asHTMLInputElement;constsearchBox=newgoogle.maps.places.SearchBox(input);map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);// Bias the SearchBox results towards current map's viewport.map.addListener("bounds_changed",()=>{searchBox.setBounds(map.getBounds()asgoogle.maps.LatLngBounds);});letmarkers:google.maps.Marker[]=[];// Listen for the event fired when the user selects a prediction and retrieve// more details for that place.searchBox.addListener("places_changed",()=>{constplaces=searchBox.getPlaces();if(places.length==0){return;}// Clear out the old markers.markers.forEach((marker)=>{marker.setMap(null);});markers=[];// For each place, get the icon, name and location.constbounds=newgoogle.maps.LatLngBounds();places.forEach((place)=>{if(!place.geometry||!place.geometry.location){console.log("Returned place contains no geometry");return;}consticon={url:place.iconasstring,size:newgoogle.maps.Size(71,71),origin:newgoogle.maps.Point(0,0),anchor:newgoogle.maps.Point(17,34),scaledSize:newgoogle.maps.Size(25,25),};// Create a marker for each place.markers.push(newgoogle.maps.Marker({map,icon,title:place.name,position:place.geometry.location,}));if(place.geometry.viewport){// Only geocodes have viewport.bounds.union(place.geometry.viewport);}else{bounds.extend(place.geometry.location);}});map.fitBounds(bounds);});}declareglobal{interfaceWindow{initAutocomplete:()=>void;}}window.initAutocomplete=initAutocomplete;
// This example adds a search box to a map, using the Google Place Autocomplete// feature. People can enter geographical searches. The search box will return a// pick list containing a mix of places and predicted search terms.// 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">functioninitAutocomplete(){constmap=newgoogle.maps.Map(document.getElementById("map"),{center:{lat:-33.8688,lng:151.2195},zoom:13,mapTypeId:"roadmap",});// Create the search box and link it to the UI element.constinput=document.getElementById("pac-input");constsearchBox=newgoogle.maps.places.SearchBox(input);map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);// Bias the SearchBox results towards current map's viewport.map.addListener("bounds_changed",()=>{searchBox.setBounds(map.getBounds());});letmarkers=[];// Listen for the event fired when the user selects a prediction and retrieve// more details for that place.searchBox.addListener("places_changed",()=>{constplaces=searchBox.getPlaces();if(places.length==0){return;}// Clear out the old markers.markers.forEach((marker)=>{marker.setMap(null);});markers=[];// For each place, get the icon, name and location.constbounds=newgoogle.maps.LatLngBounds();places.forEach((place)=>{if(!place.geometry||!place.geometry.location){console.log("Returned place contains no geometry");return;}consticon={url:place.icon,size:newgoogle.maps.Size(71,71),origin:newgoogle.maps.Point(0,0),anchor:newgoogle.maps.Point(17,34),scaledSize:newgoogle.maps.Size(25,25),};// Create a marker for each place.markers.push(newgoogle.maps.Marker({map,icon,title:place.name,position:place.geometry.location,}),);if(place.geometry.viewport){// Only geocodes have viewport.bounds.union(place.geometry.viewport);}else{bounds.extend(place.geometry.location);}});map.fitBounds(bounds);});}window.initAutocomplete=initAutocomplete;
/* * 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;}#description{font-family:Roboto;font-size:15px;font-weight:300;}#infowindow-content.title{font-weight:bold;}#infowindow-content{display:none;}#map#infowindow-content{display:inline;}.pac-card{background-color:#fff;border:0;border-radius:2px;box-shadow:01px4px-1pxrgba(0,0,0,0.3);margin:10px;padding:00.5em;font:40018pxRoboto,Arial,sans-serif;overflow:hidden;font-family:Roboto;padding:0;}#pac-container{padding-bottom:12px;margin-right:12px;}.pac-controls{display:inline-block;padding:5px11px;}.pac-controlslabel{font-family:Roboto;font-size:13px;font-weight:300;}#pac-input{background-color:#fff;font-family:Roboto;font-size:15px;font-weight:300;margin-left:12px;padding:011px013px;text-overflow:ellipsis;width:400px;}#pac-input:focus{border-color:#4d90fe;}#title{color:#fff;background-color:#4d90fe;font-size:25px;font-weight:500;padding:6px12px;}#target{width:345px;}
<html>
<head>
<title>Places Search Box</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<input
id="pac-input"
class="controls"
type="text"
placeholder="Search Box"
/>
<div id="map"></div>
<!--
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=initAutocomplete&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 example demonstrates adding a search box to a map using the Google Places Autocomplete feature.\u003c/p\u003e\n"],["\u003cp\u003eUsers can enter geographical searches, receiving a pick list of places and predicted search terms.\u003c/p\u003e\n"],["\u003cp\u003eThe search box results are biased towards the current map's viewport for better relevance.\u003c/p\u003e\n"],["\u003cp\u003eMarkers are placed on the map for each selected place, with the map automatically adjusting to fit the selected locations.\u003c/p\u003e\n"],["\u003cp\u003eThe example provides code snippets in TypeScript, JavaScript, CSS, and HTML, allowing for easy implementation.\u003c/p\u003e\n"]]],[],null,["This example creates a map with a search box for users to enter geographical\nsearches. The search box returns a pick list that includes both places and\npredicted search terms.\n\nRead the\n[documentation](/maps/documentation/javascript/places-autocomplete#places_searchbox). \n\nTypeScript \n\n```typescript\n// This example adds a search box to a map, using the Google Place Autocomplete\n// feature. People can enter geographical searches. The search box will return a\n// pick list containing a mix of places and predicted search terms.\n\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\n\nfunction initAutocomplete() {\n const map = new google.maps.Map(\n document.getElementById(\"map\") as HTMLElement,\n {\n center: { lat: -33.8688, lng: 151.2195 },\n zoom: 13,\n mapTypeId: \"roadmap\",\n }\n );\n\n // Create the search box and link it to the UI element.\n const input = document.getElementById(\"pac-input\") as HTMLInputElement;\n const searchBox = new google.maps.places.SearchBox(input);\n\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);\n\n // Bias the SearchBox results towards current map's viewport.\n map.addListener(\"bounds_changed\", () =\u003e {\n searchBox.setBounds(map.getBounds() as google.maps.LatLngBounds);\n });\n\n let markers: google.maps.Marker[] = [];\n\n // Listen for the event fired when the user selects a prediction and retrieve\n // more details for that place.\n searchBox.addListener(\"places_changed\", () =\u003e {\n const places = searchBox.getPlaces();\n\n if (places.length == 0) {\n return;\n }\n\n // Clear out the old markers.\n markers.forEach((marker) =\u003e {\n marker.setMap(null);\n });\n markers = [];\n\n // For each place, get the icon, name and location.\n const bounds = new google.maps.LatLngBounds();\n\n places.forEach((place) =\u003e {\n if (!place.geometry || !place.geometry.location) {\n console.log(\"Returned place contains no geometry\");\n return;\n }\n\n const icon = {\n url: place.icon as string,\n size: new google.maps.Size(71, 71),\n origin: new google.maps.Point(0, 0),\n anchor: new google.maps.Point(17, 34),\n scaledSize: new google.maps.Size(25, 25),\n };\n\n // Create a marker for each place.\n markers.push(\n new google.maps.Marker({\n map,\n icon,\n title: place.name,\n position: place.geometry.location,\n })\n );\n\n if (place.geometry.viewport) {\n // Only geocodes have viewport.\n bounds.union(place.geometry.viewport);\n } else {\n bounds.extend(place.geometry.location);\n }\n });\n map.fitBounds(bounds);\n });\n}\n\ndeclare global {\n interface Window {\n initAutocomplete: () =\u003e void;\n }\n}\nwindow.initAutocomplete = initAutocomplete;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/places-searchbox/index.ts#L9-L100\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 adds a search box to a map, using the Google Place Autocomplete\n// feature. People can enter geographical searches. The search box will return a\n// pick list containing a mix of places and predicted search terms.\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 initAutocomplete() {\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n center: { lat: -33.8688, lng: 151.2195 },\n zoom: 13,\n mapTypeId: \"roadmap\",\n });\n // Create the search box and link it to the UI element.\n const input = document.getElementById(\"pac-input\");\n const searchBox = new google.maps.places.SearchBox(input);\n\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);\n // Bias the SearchBox results towards current map's viewport.\n map.addListener(\"bounds_changed\", () =\u003e {\n searchBox.setBounds(map.getBounds());\n });\n\n let markers = [];\n\n // Listen for the event fired when the user selects a prediction and retrieve\n // more details for that place.\n searchBox.addListener(\"places_changed\", () =\u003e {\n const places = searchBox.getPlaces();\n\n if (places.length == 0) {\n return;\n }\n\n // Clear out the old markers.\n markers.forEach((marker) =\u003e {\n marker.setMap(null);\n });\n markers = [];\n\n // For each place, get the icon, name and location.\n const bounds = new google.maps.LatLngBounds();\n\n places.forEach((place) =\u003e {\n if (!place.geometry || !place.geometry.location) {\n console.log(\"Returned place contains no geometry\");\n return;\n }\n\n const icon = {\n url: place.icon,\n size: new google.maps.Size(71, 71),\n origin: new google.maps.Point(0, 0),\n anchor: new google.maps.Point(17, 34),\n scaledSize: new google.maps.Size(25, 25),\n };\n\n // Create a marker for each place.\n markers.push(\n new google.maps.Marker({\n map,\n icon,\n title: place.name,\n position: place.geometry.location,\n }),\n );\n if (place.geometry.viewport) {\n // Only geocodes have viewport.\n bounds.union(place.geometry.viewport);\n } else {\n bounds.extend(place.geometry.location);\n }\n });\n map.fitBounds(bounds);\n });\n}\n\nwindow.initAutocomplete = initAutocomplete;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-searchbox/docs/index.js#L8-L86\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#description {\n font-family: Roboto;\n font-size: 15px;\n font-weight: 300;\n}\n\n#infowindow-content .title {\n font-weight: bold;\n}\n\n#infowindow-content {\n display: none;\n}\n\n#map #infowindow-content {\n display: inline;\n}\n\n.pac-card {\n background-color: #fff;\n border: 0;\n border-radius: 2px;\n box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);\n margin: 10px;\n padding: 0 0.5em;\n font: 400 18px Roboto, Arial, sans-serif;\n overflow: hidden;\n font-family: Roboto;\n padding: 0;\n}\n\n#pac-container {\n padding-bottom: 12px;\n margin-right: 12px;\n}\n\n.pac-controls {\n display: inline-block;\n padding: 5px 11px;\n}\n\n.pac-controls label {\n font-family: Roboto;\n font-size: 13px;\n font-weight: 300;\n}\n\n#pac-input {\n background-color: #fff;\n font-family: Roboto;\n font-size: 15px;\n font-weight: 300;\n margin-left: 12px;\n padding: 0 11px 0 13px;\n text-overflow: ellipsis;\n width: 400px;\n}\n\n#pac-input:focus {\n border-color: #4d90fe;\n}\n\n#title {\n color: #fff;\n background-color: #4d90fe;\n font-size: 25px;\n font-weight: 500;\n padding: 6px 12px;\n}\n\n#target {\n width: 345px;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-searchbox/docs/style.css#L7-L98\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003ePlaces Search Box\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 \u003cinput\n id=\"pac-input\"\n class=\"controls\"\n type=\"text\"\n placeholder=\"Search Box\"\n /\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\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=initAutocomplete&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-searchbox/docs/index.html#L8-L36\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/places-searchbox/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-searchbox&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-searchbox 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"]]