It's the 15th anniversary of Google Maps Platform - Check out the latest news and announcements

Place ID Geocoder

The Place ID Geocoder sample uses the Place Autocomplete widget to allow a user to search for a place. After it retrieves the place details, the sample marks the place on the map, then reverse geocodes the place ID to get the place's geographic coordinates. Finally, the sample displays an info window that contains details about the place (name, place ID, and formatted address).

Read the documentation.

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">

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;

  const autocomplete = new google.maps.places.Autocomplete(input);

  autocomplete.bindTo("bounds", map);

  // Specify just the place data fields that you need.
  autocomplete.setFields(["place_id", "geometry", "name", "formatted_address"]);

  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 geocoder = new google.maps.Geocoder();

  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.place_id) {
      return;
    }
    geocoder.geocode({ placeId: place.place_id }, (results, status) => {
      if (status !== "OK") {
        window.alert("Geocoder failed due to: " + status);
        return;
      }

      map.setZoom(11);
      map.setCenter(results[0].geometry.location);

      // Set the position of the marker using the place ID and location.
      // @ts-ignore TODO(jpoehnelt) 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);
    });
  });
}

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">
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");
  const autocomplete = new google.maps.places.Autocomplete(input);
  autocomplete.bindTo("bounds", map);
  // Specify just the place data fields that you need.
  autocomplete.setFields(["place_id", "geometry", "name", "formatted_address"]);
  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 geocoder = new google.maps.Geocoder();
  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.place_id) {
      return;
    }
    geocoder.geocode({ placeId: place.place_id }, (results, status) => {
      if (status !== "OK") {
        window.alert("Geocoder failed due to: " + status);
        return;
      }
      map.setZoom(11);
      map.setCenter(results[0].geometry.location);
      // Set the position of the marker using the place ID and location.

      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);
    });
  });
}

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

<!DOCTYPE html>
<html>
  <head>
    <title>Place ID Geocoder</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <script
      src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=places&v=weekly"
      defer
    ></script>
    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script src="./app.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>
  </body>
</html>

All

<!DOCTYPE html>
<html>
  <head>
    <title>Place ID Geocoder</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <script
      src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=places&v=weekly"
      defer
    ></script>
    <style type="text/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;
      }
    </style>
    <script>
      "use strict";

      // 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">
      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");
        const autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo("bounds", map); // Specify just the place data fields that you need.

        autocomplete.setFields([
          "place_id",
          "geometry",
          "name",
          "formatted_address"
        ]);
        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 geocoder = new google.maps.Geocoder();
        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.place_id) {
            return;
          }

          geocoder.geocode(
            {
              placeId: place.place_id
            },
            (results, status) => {
              if (status !== "OK") {
                window.alert("Geocoder failed due to: " + status);
                return;
              }

              map.setZoom(11);
              map.setCenter(results[0].geometry.location); // Set the position of the marker using the place ID and location.

              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);
            }
          );
        });
      }
    </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>
  </body>
</html>
"use strict"; // 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=AIzaSyBIwzALxUPNbatRBj3Xi1Uhp0fFzwWNBkE&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"); const autocomplete = new google.maps.places.Autocomplete(input); autocomplete.bindTo("bounds", map); // Specify just the place data fields that you need. autocomplete.setFields(["place_id", "geometry", "name", "formatted_address"]); 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 geocoder = new google.maps.Geocoder(); 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.place_id) { return; } geocoder.geocode( { placeId: place.place_id }, (results, status) => { if (status !== "OK") { window.alert("Geocoder failed due to: " + status); return; } map.setZoom(11); map.setCenter(results[0].geometry.location); // Set the position of the marker using the place ID and location. 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); } ); }); }
/* 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; }
<!DOCTYPE html> <html> <head> <title>Place ID Geocoder</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBIwzALxUPNbatRBj3Xi1Uhp0fFzwWNBkE&callback=initMap&libraries=places&v=weekly" defer ></script> <!-- jsFiddle will insert css and js --> </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> </body> </html>

Create a starter application from sample

A skeleton starter application using TypeScript, Webpack, and Babel can be generated from this sample using one of the methods below.

Run Locally

Node.js is required to run this sample locally. Follow these instructions to install Node.js and NPM.

npm i -g '@googlemaps/js-samples'
googlemaps-js-samples init places-placeid-geocoder DESTINATION_FOLDER

Run in Google Cloud Shell

Google Cloud Shell is an interactive shell environment for Google Cloud Platform that makes it easy for you to learn and experiment with GCP and manage your projects and resources from your web browser.

Run in Cloud Shell