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.
Read the documentation.
TypeScript
// 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"> function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { center: { lat: -33.8688, lng: 151.2195 }, zoom: 13, } ); const input = document.getElementById("pac-input") as HTMLInputElement; // Specify just the place data fields that you need. const autocomplete = new google.maps.places.Autocomplete(input, { fields: ["place_id", "geometry", "formatted_address", "name"], }); autocomplete.bindTo("bounds", map); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); const infowindow = new google.maps.InfoWindow(); const infowindowContent = document.getElementById( "infowindow-content" ) as HTMLElement; infowindow.setContent(infowindowContent); const marker = new google.maps.Marker({ map: map }); marker.addListener("click", () => { infowindow.open(map, marker); }); autocomplete.addListener("place_changed", () => { infowindow.close(); const place = 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") as HTMLElement ).textContent = place.name as string; ( infowindowContent.children.namedItem("place-id") as HTMLElement ).textContent = place.place_id as string; ( infowindowContent.children.namedItem("place-address") as HTMLElement ).textContent = place.formatted_address as string; infowindow.open(map, marker); }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// 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"> function initMap() { const map = new google.maps.Map(document.getElementById("map"), { center: { lat: -33.8688, lng: 151.2195 }, zoom: 13, }); const input = document.getElementById("pac-input"); // Specify just the place data fields that you need. const autocomplete = new google.maps.places.Autocomplete(input, { fields: ["place_id", "geometry", "formatted_address", "name"], }); autocomplete.bindTo("bounds", map); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); const infowindow = new google.maps.InfoWindow(); const infowindowContent = document.getElementById("infowindow-content"); infowindow.setContent(infowindowContent); const marker = new google.maps.Marker({ map: map }); marker.addListener("click", () => { infowindow.open(map, marker); }); autocomplete.addListener("place_changed", () => { infowindow.close(); const place = 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;
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: 1px solid transparent; box-shadow: 0 2px 6px rgba(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: 0 11px 0 13px; 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 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>
Try Sample
Clone Sample
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.
git clone -b sample-places-placeid-finder https://github.com/googlemaps/js-samples.git
cd js-samples
npm i
npm start
Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME
.
git checkout sample-SAMPLE_NAME
npm i
npm start