Skip to content

Commit f76c879

Browse files
committed
feat: Migrates the Place AC data simple sample.
1 parent b3d91cc commit f76c879

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 } = await google.maps.importLibrary("places") as google.maps.PlacesLibrary;
10+
11+
// [START maps_place_autocomplete_data_simple_request]
12+
// Add an initial request body.
13+
// [START maps_place_autocomplete_data_simple_request_body]
14+
let request = {
15+
input: "Tadi",
16+
locationRestriction: { west: -122.44, north: 37.8, east: -122.39, south: 37.78 },
17+
origin: { lat: 37.7893, lng: -122.4039 },
18+
includedPrimaryTypes: ["restaurant"],
19+
language: "en-US",
20+
region: "us",
21+
};
22+
// [END maps_place_autocomplete_data_simple_request_body]
23+
24+
// [START maps_place_autocomplete_data_simple_token]
25+
// Create a session token.
26+
const token = new AutocompleteSessionToken();
27+
// Add the token to the request.
28+
// @ts-ignore
29+
request.sessionToken = token;
30+
// [END maps_place_autocomplete_data_simple_token]
31+
// [END maps_place_autocomplete_data_simple_request]
32+
// [START maps_place_autocomplete_data_simple_get_suggestions]
33+
// Fetch autocomplete suggestions.
34+
const { suggestions } = await AutocompleteSuggestion.fetchAutocompleteSuggestions(request);
35+
36+
const title = document.getElementById('title') as HTMLElement;
37+
title.appendChild(document.createTextNode('Query predictions for "' + request.input + '":'));
38+
39+
for (let suggestion of suggestions) {
40+
const placePrediction = suggestion.placePrediction;
41+
42+
// Create a new list element.
43+
const listItem = document.createElement('li');
44+
const resultsElement = document.getElementById("results") as HTMLElement;
45+
listItem.appendChild(document.createTextNode(placePrediction!.text.toString()));
46+
resultsElement.appendChild(listItem);
47+
}
48+
// [END maps_place_autocomplete_data_simple_get_suggestions]
49+
50+
// [START maps_place_autocomplete_data_simple_prediction]
51+
let place = suggestions[0].placePrediction!.toPlace(); // Get first predicted place.
52+
// [START maps_place_autocomplete_data_simple_fetch]
53+
await place.fetchFields({
54+
fields: ['displayName', 'formattedAddress'],
55+
});
56+
// [END maps_place_autocomplete_data_simple_fetch]
57+
58+
const placeInfo = document.getElementById("prediction") as HTMLElement;
59+
placeInfo.textContent = 'First predicted place: ' + place.displayName + ': ' + place.formattedAddress;
60+
// [END maps_place_autocomplete_data_simple_prediction]
61+
}
62+
63+
init();
64+
// [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)