|
1 | | -/* |
| 1 | + /* |
2 | 2 | * @license |
3 | 3 | * Copyright 2025 Google LLC. All Rights Reserved. |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
| 6 | +// [START maps_place_autocomplete_basic_map] |
6 | 7 |
|
| 8 | +const placeAutocompleteElement = document.querySelector( |
| 9 | + 'gmp-basic-place-autocomplete', |
| 10 | +) as any; |
| 11 | +const placeDetailsElement = document.querySelector( |
| 12 | + 'gmp-place-details-compact', |
| 13 | +) as any; |
| 14 | +const placeDetailsParent = placeDetailsElement.parentElement as HTMLElement; |
| 15 | +const mapDiv = document.getElementById('map-container') as HTMLElement; |
| 16 | +const center: google.maps.LatLngLiteral = { lat: 40.749933, lng: -73.98633 }; // New York City |
7 | 17 |
|
8 | | -// [START maps_place_autocomplete_basic_map] |
9 | | -const mapContainer = document.getElementById("map-container") as any; |
10 | | -const autocompleteElement = document.querySelector('gmp-basic-place-autocomplete') as any; |
11 | | -const detailsElement = document.querySelector('gmp-place-details-compact') as any; |
12 | | -const mapElement = document.querySelector('gmp-map') as any; |
13 | | -const advancedMarkerElement = document.querySelector('gmp-advanced-marker') as any; |
14 | 18 |
|
15 | | -let center = { lat: 40.749933, lng: -73.98633 }; // New York City |
| 19 | +async function initMap(): Promise<void> { |
| 20 | + // Asynchronously load required libraries from the Google Maps JS API. |
| 21 | + await google.maps.importLibrary('places'); |
| 22 | + const { AdvancedMarkerElement } = await google.maps.importLibrary('marker') as typeof google.maps.marker; |
| 23 | + const { Map, InfoWindow } = await google.maps.importLibrary('maps') as any; |
| 24 | + |
| 25 | + // Set the initial location bias for the autocomplete element. |
| 26 | + placeAutocompleteElement.locationBias = center; |
16 | 27 |
|
17 | | -async function initMap(): Promise<void>{ |
18 | | - //@ts-ignore |
19 | | - const {BasicPlaceAutocompleteElement, PlaceDetailsElement} = await google.maps.importLibrary('places'); |
20 | | - //@ts-ignore |
21 | | - const {AdvancedMarkerElement} = await google.maps.importLibrary('marker'); |
22 | | - //@ts-ignore |
23 | | - const {LatLngBounds} = await google.maps.importLibrary('core'); |
24 | | - |
25 | | - // Set the initial map location and autocomplete location bias |
26 | | - mapElement.center = center |
27 | | - autocompleteElement.locationBias = center; |
| 28 | + // Create the map object with specified options. |
| 29 | + const map: google.maps.Map = new Map(mapDiv, { |
| 30 | + zoom: 12, |
| 31 | + center: center, |
| 32 | + mapId: 'DEMO_MAP_ID', |
| 33 | + clickableIcons: false, |
| 34 | + mapTypeControl: false, |
| 35 | + streetViewControl: false, |
| 36 | + }); |
28 | 37 |
|
29 | | - // Get the underlying google.maps.Map object to add listeners |
30 | | - const map = mapElement.innerMap; |
| 38 | + // Create an advanced marker to show the location of a selected place. |
| 39 | + const advancedMarkerElement: google.maps.marker.AdvancedMarkerElement = new AdvancedMarkerElement({ |
| 40 | + map: map, |
| 41 | + }); |
31 | 42 |
|
32 | | - // Add the listener tochange locationBias to locationRestriction when the map moves |
33 | | - map.addListener('bounds_changed', () => { |
34 | | - autocompleteElement.locationBias = null; |
35 | | - autocompleteElement.locationRestriction = map.getBounds(); |
36 | | - console.log("bias changed to restriction") |
| 43 | + // Create an InfoWindow to hold the place details component. |
| 44 | + const infoWindow: google.maps.InfoWindow = new InfoWindow({ |
| 45 | + minWidth: 360, |
| 46 | + disableAutoPan: true, |
| 47 | + closeButton: false, |
| 48 | + headerDisabled: true, |
| 49 | + pixelOffset: new google.maps.Size(0, -10), |
37 | 50 | }); |
38 | 51 |
|
| 52 | + |
39 | 53 | // [START maps_place_autocomplete_basic_map_listener] |
40 | | - // Add the listener to update the Place Request element when the user selects a prediction |
41 | | - autocompleteElement.addEventListener('gmp-select', async (event) => { |
42 | | - const placeDetailsRequest = document.querySelector('gmp-place-details-place-request') as any; |
| 54 | + // Event listener for when a place is selected from the autocomplete list. |
| 55 | + placeAutocompleteElement.addEventListener('gmp-select', (event: any) => { |
| 56 | + |
| 57 | + // Reset marker and InfoWindow, and prepare the details element. |
| 58 | + placeDetailsParent.appendChild(placeDetailsElement); |
| 59 | + advancedMarkerElement.position = null; |
| 60 | + infoWindow.close(); |
| 61 | + |
| 62 | + // Request details for the selected place. |
| 63 | + const placeDetailsRequest = placeDetailsElement.querySelector( |
| 64 | + 'gmp-place-details-place-request', |
| 65 | + ) as any; |
43 | 66 | placeDetailsRequest.place = event.place.id; |
44 | 67 | }); |
45 | 68 | // [END maps_place_autocomplete_basic_map_listener] |
46 | 69 |
|
47 | | - // Add the listener to update the marker when the Details element loads |
48 | | - detailsElement.addEventListener('gmp-load', async () => { |
49 | | - const location = detailsElement.place.location; |
50 | | - detailsElement.style.display = "block" |
| 70 | + // Event listener for when the place details have finished loading. |
| 71 | + placeDetailsElement.addEventListener('gmp-load', () => { |
| 72 | + const location = placeDetailsElement.place.location as google.maps.LatLng; |
| 73 | + |
| 74 | + // Position the marker and open the InfoWindow at the place's location. |
51 | 75 | advancedMarkerElement.position = location; |
52 | | - advancedMarkerElement.content = detailsElement; |
53 | | - if (detailsElement.place.viewport) { |
54 | | - map.fitBounds(detailsElement.place.viewport); |
55 | | - } else { |
56 | | - map.setCenter(location); |
57 | | - map.setZoom(17); |
58 | | - } |
| 76 | + infoWindow.setContent(placeDetailsElement); |
| 77 | + infoWindow.open({ |
| 78 | + map, |
| 79 | + anchor: advancedMarkerElement, |
| 80 | + }); |
| 81 | + map.setCenter(location); |
| 82 | + placeDetailsElement.focus(); |
| 83 | + }); |
| 84 | + |
| 85 | + // Event listener to close the InfoWindow when the map is clicked. |
| 86 | + map.addListener('click', (): void => { |
| 87 | + infoWindow.close(); |
| 88 | + advancedMarkerElement.position = null; |
| 89 | + }); |
| 90 | + |
| 91 | + // Event listener for when the map finishes moving (panning or zooming). |
| 92 | + map.addListener('idle', (): void => { |
| 93 | + const newCenter = map.getCenter() as google.maps.LatLng; |
| 94 | + |
| 95 | + // Update the autocomplete's location bias to a 10km radius around the new map center. |
| 96 | + placeAutocompleteElement.locationBias = new google.maps.Circle({ |
| 97 | + center: { |
| 98 | + lat: newCenter.lat(), |
| 99 | + lng: newCenter.lng(), |
| 100 | + }, |
| 101 | + radius: 10000, // 10km in meters |
| 102 | + }); |
59 | 103 | }); |
60 | 104 | } |
61 | 105 |
|
62 | 106 | initMap(); |
63 | 107 | // [END maps_place_autocomplete_basic_map] |
64 | | - |
|
0 commit comments