Skip to content

Commit 6fd32d6

Browse files
authored
feat: Migrates the Place AC data simple sample. (#922)
* feat: Migrates the Place AC data simple sample. * Apply review comments * Fix DOCTYPE declaration and add trailing line break * Add newline at end of style.css
1 parent 08ddcc0 commit 6fd32d6

File tree

7 files changed

+208
-0
lines changed

7 files changed

+208
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Google Maps JavaScript Sample
2+
3+
## place-autocomplete-data-simple
4+
5+
The place-autocomplete-data-simple sample demonstrates making a single request for Place predictions,
6+
then requests Place Details for the first result.
7+
8+
## Setup
9+
10+
### Before starting run:
11+
12+
`npm i`
13+
14+
### Run an example on a local web server
15+
16+
`cd samples/place-autocomplete-data-simple`
17+
`npm start`
18+
19+
### Build an individual example
20+
21+
`cd samples/place-autocomplete-data-simple`
22+
`npm run build`
23+
24+
From 'samples':
25+
26+
`npm run build --workspace=place-autocomplete-data-simple/`
27+
28+
### Build all of the examples.
29+
30+
From 'samples':
31+
32+
`npm run build-all`
33+
34+
### Run lint to check for problems
35+
36+
`cd samples/place-autocomplete-data-simple`
37+
`npx eslint index.ts`
38+
39+
## Feedback
40+
41+
For feedback related to this sample, please open a new issue on
42+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<!--
3+
@license
4+
Copyright 2025 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_place_autocomplete_data_simple] -->
8+
<html>
9+
<head>
10+
<title>Place Autocomplete Data API Predictions</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<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))})
16+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
17+
</head>
18+
<body>
19+
<!-- [START maps_place_autocomplete_data_simple_html] -->
20+
<div id="title"></div>
21+
<ul id="results"></ul>
22+
<p><span id="prediction"></span></p>
23+
<img
24+
class="powered-by-google"
25+
src="./powered_by_google_on_white.png"
26+
alt="Powered by Google"
27+
/>
28+
<!-- [END maps_place_autocomplete_data_simple_html] -->
29+
30+
</body>
31+
</html>
32+
<!-- [END maps_place_autocomplete_data_simple] -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_place_autocomplete_data_simple]
8+
async function init() {
9+
const { Place, AutocompleteSessionToken, AutocompleteSuggestion } =
10+
(await google.maps.importLibrary("places")) as google.maps.PlacesLibrary;
11+
12+
// [START maps_place_autocomplete_data_simple_request]
13+
// Add an initial request body.
14+
// [START maps_place_autocomplete_data_simple_request_body]
15+
let request = {
16+
input: "Tadi",
17+
locationRestriction: {
18+
west: -122.44,
19+
north: 37.8,
20+
east: -122.39,
21+
south: 37.78,
22+
},
23+
origin: { lat: 37.7893, lng: -122.4039 },
24+
includedPrimaryTypes: ["restaurant"],
25+
language: "en-US",
26+
region: "us",
27+
};
28+
// [END maps_place_autocomplete_data_simple_request_body]
29+
30+
// [START maps_place_autocomplete_data_simple_token]
31+
// Create a session token.
32+
const token = new AutocompleteSessionToken();
33+
// Add the token to the request.
34+
// @ts-ignore
35+
request.sessionToken = token;
36+
// [END maps_place_autocomplete_data_simple_token]
37+
// [END maps_place_autocomplete_data_simple_request]
38+
// [START maps_place_autocomplete_data_simple_get_suggestions]
39+
// Fetch autocomplete suggestions.
40+
const { suggestions } =
41+
await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);
42+
43+
const title = document.getElementById("title") as HTMLElement;
44+
title.appendChild(
45+
document.createTextNode('Query predictions for "' + request.input + '":')
46+
);
47+
48+
const resultsElement = document.getElementById("results") as HTMLElement;
49+
50+
for (let suggestion of suggestions) {
51+
const placePrediction = suggestion.placePrediction;
52+
53+
// Create a new list element.
54+
const listItem = document.createElement("li");
55+
56+
listItem.appendChild(
57+
document.createTextNode(placePrediction!.text.toString())
58+
);
59+
resultsElement.appendChild(listItem);
60+
}
61+
// [END maps_place_autocomplete_data_simple_get_suggestions]
62+
63+
// [START maps_place_autocomplete_data_simple_prediction]
64+
let place = suggestions[0].placePrediction!.toPlace(); // Get first predicted place.
65+
// [START maps_place_autocomplete_data_simple_fetch]
66+
await place.fetchFields({
67+
fields: ["displayName", "formattedAddress"],
68+
});
69+
// [END maps_place_autocomplete_data_simple_fetch]
70+
71+
const placeInfo = document.getElementById("prediction") as HTMLElement;
72+
placeInfo.textContent =
73+
`First predicted place: ${place.displayName}: ${place.formattedAddress}`;
74+
// [END maps_place_autocomplete_data_simple_prediction]
75+
}
76+
77+
init();
78+
// [END maps_place_autocomplete_data_simple]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/place-autocomplete-data-simple",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"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",
6+
"test": "tsc && npm run build:vite --workspace=.",
7+
"start": "tsc && vite build --base './' && vite",
8+
"build:vite": "vite build --base './'",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
13+
}
14+
}
8.96 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_place_autocomplete_data_simple] */
7+
/*
8+
* Always set the map height explicitly to define the size of the div element
9+
* that contains the map.
10+
*/
11+
#map {
12+
height: 100%;
13+
}
14+
15+
/*
16+
* Optional: Makes the sample page fill the window.
17+
*/
18+
html,
19+
body {
20+
height: 100%;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
25+
/* [END maps_place_autocomplete_data_simple] */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"strict": true,
6+
"noImplicitAny": false,
7+
"lib": [
8+
"es2015",
9+
"esnext",
10+
"es6",
11+
"dom",
12+
"dom.iterable"
13+
],
14+
"moduleResolution": "Node",
15+
"jsx": "preserve"
16+
}
17+
}

0 commit comments

Comments
 (0)