|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 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 | + |
| 15 | +let center = { lat: 40.749933, lng: -73.98633 }; // New York City |
| 16 | + |
| 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 | + |
| 29 | + // Get the underlying google.maps.Map object to add listeners |
| 30 | + const map = mapElement.innerMap; |
| 31 | + |
| 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") |
| 37 | + }); |
| 38 | + |
| 39 | + // [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; |
| 43 | + placeDetailsRequest.place = event.place.id; |
| 44 | + }); |
| 45 | + // [END maps_place_autocomplete_basic_map_listener] |
| 46 | + |
| 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" |
| 51 | + 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 | + } |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +initMap(); |
| 63 | +// [END maps_place_autocomplete_basic_map] |
| 64 | + |
0 commit comments