|
| 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] |
0 commit comments