Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions samples/place-autocomplete-data-simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Google Maps JavaScript Sample

## place-autocomplete-data-simple

The place-autocomplete-data-simple sample demonstrates making a single request for Place predictions,
then requests Place Details for the first result.

## Setup

### Before starting run:

`npm i`

### Run an example on a local web server

`cd samples/place-autocomplete-data-simple`
`npm start`

### Build an individual example

`cd samples/place-autocomplete-data-simple`
`npm run build`

From 'samples':

`npm run build --workspace=place-autocomplete-data-simple/`

### Build all of the examples.

From 'samples':

`npm run build-all`

### Run lint to check for problems

`cd samples/place-autocomplete-data-simple`
`npx eslint index.ts`

## Feedback

For feedback related to this sample, please open a new issue on
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
32 changes: 32 additions & 0 deletions samples/place-autocomplete-data-simple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!-- [START maps_place_autocomplete_data_simple] -->
<html>
<head>
<title>Place Autocomplete Data API Predictions</title>

<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<!-- prettier-ignore -->
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
</head>
<body>
<!-- [START maps_place_autocomplete_data_simple_html] -->
<div id="title"></div>
<ul id="results"></ul>
<p><span id="prediction"></span></p>
<img
class="powered-by-google"
src="./powered_by_google_on_white.png"
alt="Powered by Google"
/>
<!-- [END maps_place_autocomplete_data_simple_html] -->

</body>
</html>
<!-- [END maps_place_autocomplete_data_simple] -->
78 changes: 78 additions & 0 deletions samples/place-autocomplete-data-simple/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// [START maps_place_autocomplete_data_simple]
async function init() {
const { Place, AutocompleteSessionToken, AutocompleteSuggestion } =
(await google.maps.importLibrary("places")) as google.maps.PlacesLibrary;

// [START maps_place_autocomplete_data_simple_request]
// Add an initial request body.
// [START maps_place_autocomplete_data_simple_request_body]
let request = {
input: "Tadi",
locationRestriction: {
west: -122.44,
north: 37.8,
east: -122.39,
south: 37.78,
},
origin: { lat: 37.7893, lng: -122.4039 },
includedPrimaryTypes: ["restaurant"],
language: "en-US",
region: "us",
};
// [END maps_place_autocomplete_data_simple_request_body]

// [START maps_place_autocomplete_data_simple_token]
// Create a session token.
const token = new AutocompleteSessionToken();
// Add the token to the request.
// @ts-ignore
request.sessionToken = token;
// [END maps_place_autocomplete_data_simple_token]
// [END maps_place_autocomplete_data_simple_request]
// [START maps_place_autocomplete_data_simple_get_suggestions]
// Fetch autocomplete suggestions.
const { suggestions } =
await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);

const title = document.getElementById("title") as HTMLElement;
title.appendChild(
document.createTextNode('Query predictions for "' + request.input + '":')
);

const resultsElement = document.getElementById("results") as HTMLElement;

for (let suggestion of suggestions) {
const placePrediction = suggestion.placePrediction;

// Create a new list element.
const listItem = document.createElement("li");

listItem.appendChild(
document.createTextNode(placePrediction!.text.toString())
);
resultsElement.appendChild(listItem);
}
// [END maps_place_autocomplete_data_simple_get_suggestions]

// [START maps_place_autocomplete_data_simple_prediction]
let place = suggestions[0].placePrediction!.toPlace(); // Get first predicted place.
// [START maps_place_autocomplete_data_simple_fetch]
await place.fetchFields({
fields: ["displayName", "formattedAddress"],
});
// [END maps_place_autocomplete_data_simple_fetch]

const placeInfo = document.getElementById("prediction") as HTMLElement;
placeInfo.textContent =
`First predicted place: ${place.displayName}: ${place.formattedAddress}`;
// [END maps_place_autocomplete_data_simple_prediction]
}

init();
// [END maps_place_autocomplete_data_simple]
14 changes: 14 additions & 0 deletions samples/place-autocomplete-data-simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@js-api-samples/place-autocomplete-data-simple",
"version": "1.0.0",
"scripts": {
"build": "tsc && bash ../jsfiddle.sh place-autocomplete-data-simple && bash ../app.sh place-autocomplete-data-simple && bash ../docs.sh place-autocomplete-data-simple && npm run build:vite --workspace=. && bash ../dist.sh place-autocomplete-data-simple",
"test": "tsc && npm run build:vite --workspace=.",
"start": "tsc && vite build --base './' && vite",
"build:vite": "vite build --base './'",
"preview": "vite preview"
},
"dependencies": {

}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions samples/place-autocomplete-data-simple/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* [START maps_place_autocomplete_data_simple] */
/*
* 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;
}

/* [END maps_place_autocomplete_data_simple] */
17 changes: 17 additions & 0 deletions samples/place-autocomplete-data-simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"strict": true,
"noImplicitAny": false,
"lib": [
"es2015",
"esnext",
"es6",
"dom",
"dom.iterable"
],
"moduleResolution": "Node",
"jsx": "preserve"
}
}