Stay organized with collections
Save and categorize content based on your preferences.
The Place ID Finder sample allows a user to find a place based
upon its address, then it adds a marker for the place to the map, and displays
the place's place ID in an info window.
// This sample uses the Place Autocomplete widget to allow the user to search// for and select a place. The sample then displays an info window containing// the place ID and other information about the place that the user has// selected.// 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">functioninitMap():void{constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{center:{lat:-33.8688,lng:151.2195},zoom:13,});constinput=document.getElementById("pac-input")asHTMLInputElement;// Specify just the place data fields that you need.constautocomplete=newgoogle.maps.places.Autocomplete(input,{fields:["place_id","geometry","formatted_address","name"],});autocomplete.bindTo("bounds",map);map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);constinfowindow=newgoogle.maps.InfoWindow();constinfowindowContent=document.getElementById("infowindow-content")asHTMLElement;infowindow.setContent(infowindowContent);constmarker=newgoogle.maps.Marker({map:map});marker.addListener("click",()=>{infowindow.open(map,marker);});autocomplete.addListener("place_changed",()=>{infowindow.close();constplace=autocomplete.getPlace();if(!place.geometry||!place.geometry.location){return;}if(place.geometry.viewport){map.fitBounds(place.geometry.viewport);}else{map.setCenter(place.geometry.location);map.setZoom(17);}// Set the position of the marker using the place ID and location.// @ts-ignore This should be in @typings/googlemaps.marker.setPlace({placeId:place.place_id,location:place.geometry.location,});marker.setVisible(true);(infowindowContent.children.namedItem("place-name")asHTMLElement).textContent=place.nameasstring;(infowindowContent.children.namedItem("place-id")asHTMLElement).textContent=place.place_idasstring;(infowindowContent.children.namedItem("place-address")asHTMLElement).textContent=place.formatted_addressasstring;infowindow.open(map,marker);});}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
// This sample uses the Place Autocomplete widget to allow the user to search// for and select a place. The sample then displays an info window containing// the place ID and other information about the place that the user has// selected.// 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">functioninitMap(){constmap=newgoogle.maps.Map(document.getElementById("map"),{center:{lat:-33.8688,lng:151.2195},zoom:13,});constinput=document.getElementById("pac-input");// Specify just the place data fields that you need.constautocomplete=newgoogle.maps.places.Autocomplete(input,{fields:["place_id","geometry","formatted_address","name"],});autocomplete.bindTo("bounds",map);map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);constinfowindow=newgoogle.maps.InfoWindow();constinfowindowContent=document.getElementById("infowindow-content");infowindow.setContent(infowindowContent);constmarker=newgoogle.maps.Marker({map:map});marker.addListener("click",()=>{infowindow.open(map,marker);});autocomplete.addListener("place_changed",()=>{infowindow.close();constplace=autocomplete.getPlace();if(!place.geometry||!place.geometry.location){return;}if(place.geometry.viewport){map.fitBounds(place.geometry.viewport);}else{map.setCenter(place.geometry.location);map.setZoom(17);}// Set the position of the marker using the place ID and location.// @ts-ignore This should be in @typings/googlemaps.marker.setPlace({placeId:place.place_id,location:place.geometry.location,});marker.setVisible(true);infowindowContent.children.namedItem("place-name").textContent=place.name;infowindowContent.children.namedItem("place-id").textContent=place.place_id;infowindowContent.children.namedItem("place-address").textContent=place.formatted_address;infowindow.open(map,marker);});}window.initMap=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;}.controls{background-color:#fff;border-radius:2px;border:1pxsolidtransparent;box-shadow:02px6pxrgba(0,0,0,0.3);box-sizing:border-box;font-family:Roboto;font-size:15px;font-weight:300;height:29px;margin-left:17px;margin-top:10px;outline:none;padding:011px013px;text-overflow:ellipsis;width:400px;}.controls:focus{border-color:#4d90fe;}.title{font-weight:bold;}#infowindow-content{display:none;}#map#infowindow-content{display:inline;}
<html>
<head>
<title>Place ID Finder</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div style="display: none">
<input
id="pac-input"
class="controls"
type="text"
placeholder="Enter a location"
/>
</div>
<div id="map"></div>
<div id="infowindow-content">
<span id="place-name" class="title"></span><br />
<strong>Place ID:</strong> <span id="place-id"></span><br />
<span id="place-address"></span>
</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=initMap&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 tool uses the Google Maps Places API to locate a place based on its address.\u003c/p\u003e\n"],["\u003cp\u003eIt then places a marker on the map at the located place's coordinates.\u003c/p\u003e\n"],["\u003cp\u003eAn info window is displayed, containing the place's ID, name, and formatted address.\u003c/p\u003e\n"],["\u003cp\u003eSample code is provided in both JavaScript and TypeScript, demonstrating the functionality.\u003c/p\u003e\n"]]],[],null,["The **Place ID Finder** sample allows a user to find a place based\nupon its address, then it adds a marker for the place to the map, and displays\nthe place's place ID in an info window.\n\nRead the [documentation](/maps/documentation/javascript/places#placeid). \n\nTypeScript \n\n```typescript\n// This sample uses the Place Autocomplete widget to allow the user to search\n// for and select a place. The sample then displays an info window containing\n// the place ID and other information about the place that the user has\n// selected.\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 initMap(): void {\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 }\n );\n\n const input = document.getElementById(\"pac-input\") as HTMLInputElement;\n\n // Specify just the place data fields that you need.\n const autocomplete = new google.maps.places.Autocomplete(input, {\n fields: [\"place_id\", \"geometry\", \"formatted_address\", \"name\"],\n });\n\n autocomplete.bindTo(\"bounds\", map);\n\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);\n\n const infowindow = new google.maps.InfoWindow();\n const infowindowContent = document.getElementById(\n \"infowindow-content\"\n ) as HTMLElement;\n\n infowindow.setContent(infowindowContent);\n\n const marker = new google.maps.Marker({ map: map });\n\n marker.addListener(\"click\", () =\u003e {\n infowindow.open(map, marker);\n });\n\n autocomplete.addListener(\"place_changed\", () =\u003e {\n infowindow.close();\n\n const place = autocomplete.getPlace();\n\n if (!place.geometry || !place.geometry.location) {\n return;\n }\n\n if (place.geometry.viewport) {\n map.fitBounds(place.geometry.viewport);\n } else {\n map.setCenter(place.geometry.location);\n map.setZoom(17);\n }\n\n // Set the position of the marker using the place ID and location.\n // @ts-ignore This should be in @typings/googlemaps.\n marker.setPlace({\n placeId: place.place_id,\n location: place.geometry.location,\n });\n\n marker.setVisible(true);\n\n (\n infowindowContent.children.namedItem(\"place-name\") as HTMLElement\n ).textContent = place.name as string;\n (\n infowindowContent.children.namedItem(\"place-id\") as HTMLElement\n ).textContent = place.place_id as string;\n (\n infowindowContent.children.namedItem(\"place-address\") as HTMLElement\n ).textContent = place.formatted_address as string;\n infowindow.open(map, marker);\n });\n}\n\ndeclare global {\n interface Window {\n initMap: () =\u003e void;\n }\n}\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/places-placeid-finder/index.ts#L8-L93\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\n// This sample uses the Place Autocomplete widget to allow the user to search\n// for and select a place. The sample then displays an info window containing\n// the place ID and other information about the place that the user has\n// selected.\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 initMap() {\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n center: { lat: -33.8688, lng: 151.2195 },\n zoom: 13,\n });\n const input = document.getElementById(\"pac-input\");\n // Specify just the place data fields that you need.\n const autocomplete = new google.maps.places.Autocomplete(input, {\n fields: [\"place_id\", \"geometry\", \"formatted_address\", \"name\"],\n });\n\n autocomplete.bindTo(\"bounds\", map);\n map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);\n\n const infowindow = new google.maps.InfoWindow();\n const infowindowContent = document.getElementById(\"infowindow-content\");\n\n infowindow.setContent(infowindowContent);\n\n const marker = new google.maps.Marker({ map: map });\n\n marker.addListener(\"click\", () =\u003e {\n infowindow.open(map, marker);\n });\n autocomplete.addListener(\"place_changed\", () =\u003e {\n infowindow.close();\n\n const place = autocomplete.getPlace();\n\n if (!place.geometry || !place.geometry.location) {\n return;\n }\n\n if (place.geometry.viewport) {\n map.fitBounds(place.geometry.viewport);\n } else {\n map.setCenter(place.geometry.location);\n map.setZoom(17);\n }\n\n // Set the position of the marker using the place ID and location.\n // @ts-ignore This should be in @typings/googlemaps.\n marker.setPlace({\n placeId: place.place_id,\n location: place.geometry.location,\n });\n marker.setVisible(true);\n infowindowContent.children.namedItem(\"place-name\").textContent = place.name;\n infowindowContent.children.namedItem(\"place-id\").textContent =\n place.place_id;\n infowindowContent.children.namedItem(\"place-address\").textContent =\n place.formatted_address;\n infowindow.open(map, marker);\n });\n}\n\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-placeid-finder/docs/index.js#L7-L70\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.controls {\n background-color: #fff;\n border-radius: 2px;\n border: 1px solid transparent;\n box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);\n box-sizing: border-box;\n font-family: Roboto;\n font-size: 15px;\n font-weight: 300;\n height: 29px;\n margin-left: 17px;\n margin-top: 10px;\n outline: none;\n padding: 0 11px 0 13px;\n text-overflow: ellipsis;\n width: 400px;\n}\n\n.controls:focus {\n border-color: #4d90fe;\n}\n\n.title {\n font-weight: bold;\n}\n\n#infowindow-content {\n display: none;\n}\n\n#map #infowindow-content {\n display: inline;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/places-placeid-finder/docs/style.css#L7-L58\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003ePlace ID Finder\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 style=\"display: none\"\u003e\n \u003cinput\n id=\"pac-input\"\n class=\"controls\"\n type=\"text\"\n placeholder=\"Enter a location\"\n /\u003e\n \u003c/div\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\u003e\n \u003cdiv id=\"infowindow-content\"\u003e\n \u003cspan id=\"place-name\" class=\"title\"\u003e\u003c/span\u003e\u003cbr /\u003e\n \u003cstrong\u003ePlace ID:\u003c/strong\u003e \u003cspan id=\"place-id\"\u003e\u003c/span\u003e\u003cbr /\u003e\n \u003cspan id=\"place-address\"\u003e\u003c/span\u003e\n \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=initMap&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-placeid-finder/docs/index.html#L8-L43\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/places-placeid-finder/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-placeid-finder&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-placeid-finder 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"]]