|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_places_placeid_finder] |
| 8 | +// This sample uses the Place Autocomplete widget to allow the user to search |
| 9 | +// for and select a place. The sample then displays an info window containing |
| 10 | +// the place ID and other information about the place that the user has |
| 11 | +// selected. |
| 12 | + |
| 13 | +// This example requires the Places library. Include the libraries=places |
| 14 | +// parameter when you first load the API. For example: |
| 15 | +// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"> |
| 16 | + |
| 17 | +async function initMap(): Promise<void> { |
| 18 | + // Request needed libraries. |
| 19 | + const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ |
| 20 | + google.maps.importLibrary('maps') as Promise<google.maps.MapsLibrary>, |
| 21 | + google.maps.importLibrary( |
| 22 | + 'marker' |
| 23 | + ) as Promise<google.maps.MarkerLibrary>, |
| 24 | + google.maps.importLibrary( |
| 25 | + 'places' |
| 26 | + ) as Promise<google.maps.PlacesLibrary>, |
| 27 | + ]); |
| 28 | + |
| 29 | + const mapElement = document.querySelector( |
| 30 | + 'gmp-map' |
| 31 | + ) as google.maps.MapElement; |
| 32 | + const map = mapElement.innerMap as google.maps.Map; |
| 33 | + |
| 34 | + const placeAutocomplete = document.querySelector( |
| 35 | + 'gmp-place-autocomplete' |
| 36 | + ) as google.maps.places.PlaceAutocompleteElement; |
| 37 | + |
| 38 | + // Set the map options. |
| 39 | + map.setOptions({ |
| 40 | + clickableIcons: false, |
| 41 | + mapTypeControl: false, |
| 42 | + streetViewControl: false, |
| 43 | + }); |
| 44 | + |
| 45 | + // Use the bounds_changed event to bias results to the current map bounds. |
| 46 | + map.addListener('bounds_changed', () => { |
| 47 | + const bounds = map.getBounds(); |
| 48 | + if (bounds) { |
| 49 | + placeAutocomplete.locationBias = bounds; |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + const infowindow = new InfoWindow(); |
| 54 | + const infowindowContent = document.getElementById( |
| 55 | + 'infowindow-content' |
| 56 | + ) as HTMLElement; |
| 57 | + |
| 58 | + infowindow.setContent(infowindowContent); |
| 59 | + |
| 60 | + const marker = new AdvancedMarkerElement({ |
| 61 | + map: map, |
| 62 | + collisionBehavior: |
| 63 | + google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL, |
| 64 | + }); |
| 65 | + |
| 66 | + marker.addListener('click', () => { |
| 67 | + infowindow.open(map, marker); |
| 68 | + }); |
| 69 | + |
| 70 | + placeAutocomplete.addEventListener( |
| 71 | + 'gmp-select', |
| 72 | + async ({ placePrediction }: any) => { |
| 73 | + infowindow.close(); |
| 74 | + |
| 75 | + const place = placePrediction.toPlace(); |
| 76 | + |
| 77 | + await place.fetchFields({ |
| 78 | + fields: ['displayName', 'formattedAddress', 'location', 'id'], |
| 79 | + }); |
| 80 | + |
| 81 | + if (!place.location) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (place.viewport) { |
| 86 | + map.fitBounds(place.viewport); |
| 87 | + } else { |
| 88 | + map.setCenter(place.location); |
| 89 | + map.setZoom(17); |
| 90 | + } |
| 91 | + |
| 92 | + // Set the position of the marker using the place ID and location. |
| 93 | + marker.position = place.location; |
| 94 | + // marker.setVisible(true); // AdvancedMarkerElement is visible by default when map and position are set. |
| 95 | + |
| 96 | + ( |
| 97 | + infowindowContent.children.namedItem( |
| 98 | + 'place-name' |
| 99 | + ) as HTMLElement |
| 100 | + ).textContent = place.displayName as string; |
| 101 | + ( |
| 102 | + infowindowContent.children.namedItem('place-id') as HTMLElement |
| 103 | + ).textContent = place.id as string; |
| 104 | + ( |
| 105 | + infowindowContent.children.namedItem( |
| 106 | + 'place-address' |
| 107 | + ) as HTMLElement |
| 108 | + ).textContent = place.formattedAddress as string; |
| 109 | + infowindow.open(map, marker); |
| 110 | + } |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +initMap(); |
| 115 | +// [END maps_places_placeid_finder] |
| 116 | +export {}; |
0 commit comments