Places UI Kit: A ready-to-use library that provides room for customization and low-code development. Try it out, and share yourinput on your UI Kit experience.

Place ID Geocoder

  • This sample demonstrates using the Place Autocomplete widget to search for a place and retrieve its details.

  • It uses the retrieved place ID to reverse geocode the location and obtain geographic coordinates.

  • The sample marks the selected place on a map and displays an info window with details like name, place ID, and address.

  • This functionality requires including the Places library when loading the Google Maps JavaScript API.

ThePlace ID Geocoder sample uses thePlaceAutocomplete widget to allow a user to search for a place. After itretrieves the place details, the sample marks the place on the map, then reversegeocodes the place ID to get the place's geographic coordinates. Finally, thesample displays an info window that contains details about the place (name,place ID, and formatted address).

Read thedocumentation.

TypeScript

// This sample 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","name","formatted_address"],});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);constgeocoder=newgoogle.maps.Geocoder();constmarker=newgoogle.maps.Marker({map:map});marker.addListener("click",()=>{infowindow.open(map,marker);});autocomplete.addListener("place_changed",()=>{infowindow.close();constplace=autocomplete.getPlace();if(!place.place_id){return;}geocoder.geocode({placeId:place.place_id}).then(({results})=>{map.setZoom(11);map.setCenter(results[0].geometry.location);// Set the position of the marker using the place ID and location.// @ts-ignore TODO This should be in @typings/googlemaps.marker.setPlace({placeId:place.place_id,location:results[0].geometry.location,});marker.setVisible(true);infowindowContent.children["place-name"].textContent=place.name;infowindowContent.children["place-id"].textContent=place.place_id;infowindowContent.children["place-address"].textContent=results[0].formatted_address;infowindow.open(map,marker);}).catch((e)=>window.alert("Geocoder failed due to: "+e));});}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
Note: Read theguide on using TypeScript and Google Maps.

JavaScript

// This sample 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","name","formatted_address"],});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);constgeocoder=newgoogle.maps.Geocoder();constmarker=newgoogle.maps.Marker({map:map});marker.addListener("click",()=>{infowindow.open(map,marker);});autocomplete.addListener("place_changed",()=>{infowindow.close();constplace=autocomplete.getPlace();if(!place.place_id){return;}geocoder.geocode({placeId:place.place_id}).then(({results})=>{map.setZoom(11);map.setCenter(results[0].geometry.location);// Set the position of the marker using the place ID and location.// @ts-ignore TODO This should be in @typings/googlemaps.marker.setPlace({placeId:place.place_id,location:results[0].geometry.location,});marker.setVisible(true);infowindowContent.children["place-name"].textContent=place.name;infowindowContent.children["place-id"].textContent=place.place_id;infowindowContent.children["place-address"].textContent=results[0].formatted_address;infowindow.open(map,marker);}).catch((e)=>window.alert("Geocoder failed due to: "+e));});}window.initMap=initMap;
Note: The JavaScript is compiled from the TypeScript snippet.

CSS

/* * 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

<html>  <head>    <title>Place ID Geocoder</title>    <link rel="stylesheet" type="text/css" href="./style.css" />    <script type="module" src="./index.js"></script>  </head>  <body>    <div>      <input                      type="text"        placeholder="Enter a location"      />    </div>    <div></div>    <div>      <span></span><br />      <strong>Place ID</strong>: <span></span><br />      <span></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>

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

gitclone-bsample-places-placeid-geocoderhttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart

Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.

gitcheckoutsample-SAMPLE_NAMEnpminpmstart

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.