|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | // [START maps_place_autocomplete_map] |
8 | | -let map: google.maps.Map; |
| 8 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 9 | +const placeAutocomplete = document.querySelector( |
| 10 | + 'gmp-place-autocomplete' |
| 11 | +) as google.maps.places.PlaceAutocompleteElement; |
| 12 | +let innerMap; |
9 | 13 | let marker: google.maps.marker.AdvancedMarkerElement; |
10 | 14 | let infoWindow: google.maps.InfoWindow; |
11 | 15 | let center = { lat: 40.749933, lng: -73.98633 }; // New York City |
12 | 16 | async function initMap(): Promise<void> { |
13 | | - // Request needed libraries. |
14 | | - //@ts-ignore |
15 | | - const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([ |
16 | | - google.maps.importLibrary("marker"), |
17 | | - google.maps.importLibrary("places") |
18 | | - ]); |
| 17 | + // Request needed libraries. |
| 18 | + const [] = await Promise.all([ |
| 19 | + google.maps.importLibrary('marker'), |
| 20 | + google.maps.importLibrary('places'), |
| 21 | + ]); |
19 | 22 |
|
20 | | - // Initialize the map. |
21 | | - map = new google.maps.Map(document.getElementById('map') as HTMLElement, { |
22 | | - center, |
23 | | - zoom: 13, |
24 | | - mapId: '4504f8b37365c3d0', |
25 | | - mapTypeControl: false, |
26 | | - }); |
27 | | - // [START maps_place_autocomplete_map_add] |
28 | | - //@ts-ignore |
29 | | - const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement(); |
30 | | - //@ts-ignore |
31 | | - placeAutocomplete.id = 'place-autocomplete-input'; |
32 | | - placeAutocomplete.locationBias = center; |
| 23 | + // [START maps_place_autocomplete_map_add] |
| 24 | + // Get the inner map. |
| 25 | + innerMap = mapElement.innerMap; |
| 26 | + innerMap.setOptions({ |
| 27 | + mapTypeControl: false, |
| 28 | + }); |
33 | 29 |
|
34 | | - const card = document.getElementById('place-autocomplete-card') as HTMLElement; |
35 | | - //@ts-ignore |
36 | | - card.appendChild(placeAutocomplete); |
37 | | - map.controls[google.maps.ControlPosition.TOP_LEFT].push(card); |
38 | | - // [END maps_place_autocomplete_map_add] |
| 30 | + // Use the bounds_changed event to restrict results to the current map bounds. |
| 31 | + google.maps.event.addListener(innerMap, 'bounds_changed', async () => { |
| 32 | + placeAutocomplete.locationRestriction = innerMap.getBounds(); |
| 33 | + }); |
| 34 | + // [END maps_place_autocomplete_map_add] |
39 | 35 |
|
40 | | - // Create the marker and infowindow. |
41 | | - marker = new google.maps.marker.AdvancedMarkerElement({ |
42 | | - map, |
43 | | - }); |
44 | | - |
45 | | - infoWindow = new google.maps.InfoWindow({}); |
| 36 | + // Create the marker and infowindow. |
| 37 | + marker = new google.maps.marker.AdvancedMarkerElement({ |
| 38 | + map: innerMap, |
| 39 | + }); |
46 | 40 |
|
47 | | - // [START maps_place_autocomplete_map_listener] |
48 | | - // Add the gmp-placeselect listener, and display the results on the map. |
49 | | - //@ts-ignore |
50 | | - placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) => { |
51 | | - const place = placePrediction.toPlace(); |
52 | | - await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] }); |
| 41 | + infoWindow = new google.maps.InfoWindow({}); |
53 | 42 |
|
54 | | - // If the place has a geometry, then present it on a map. |
55 | | - if (place.viewport) { |
56 | | - map.fitBounds(place.viewport); |
57 | | - } else { |
58 | | - map.setCenter(place.location); |
59 | | - map.setZoom(17); |
60 | | - } |
| 43 | + // [START maps_place_autocomplete_map_listener] |
| 44 | + // Add the gmp-placeselect listener, and display the results on the map. |
| 45 | + //@ts-ignore |
| 46 | + placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) => { |
| 47 | + const place = placePrediction.toPlace(); |
| 48 | + await place.fetchFields({ |
| 49 | + fields: ['displayName', 'formattedAddress', 'location'], |
| 50 | + }); |
61 | 51 |
|
62 | | - let content = '<div id="infowindow-content">' + |
63 | | - '<span id="place-displayname" class="title">' + place.displayName + '</span><br />' + |
64 | | - '<span id="place-address">' + place.formattedAddress + '</span>' + |
65 | | - '</div>'; |
| 52 | + // If the place has a geometry, then present it on a map. |
| 53 | + if (place.viewport) { |
| 54 | + innerMap.fitBounds(place.viewport); |
| 55 | + } else { |
| 56 | + innerMap.setCenter(place.location); |
| 57 | + innerMap.setZoom(17); |
| 58 | + } |
66 | 59 |
|
67 | | - updateInfoWindow(content, place.location); |
68 | | - marker.position = place.location; |
69 | | - }); |
70 | | - // [END maps_place_autocomplete_map_listener] |
| 60 | + let content = document.createElement('div'); |
| 61 | + let nameText = document.createElement('span'); |
| 62 | + nameText.textContent = place.displayName; |
| 63 | + content.appendChild(nameText); |
| 64 | + content.appendChild(document.createElement('br')); |
| 65 | + let addressText = document.createElement('span'); |
| 66 | + addressText.textContent = place.formattedAddress; |
| 67 | + content.appendChild(addressText); |
| 68 | + |
| 69 | + updateInfoWindow(content, place.location); |
| 70 | + marker.position = place.location; |
| 71 | + } |
| 72 | + ); |
| 73 | + // [END maps_place_autocomplete_map_listener] |
71 | 74 | } |
72 | 75 |
|
73 | 76 | // Helper function to create an info window. |
74 | 77 | function updateInfoWindow(content, center) { |
75 | | - infoWindow.setContent(content); |
76 | | - infoWindow.setPosition(center); |
77 | | - infoWindow.open({ |
78 | | - map, |
79 | | - anchor: marker, |
80 | | - shouldFocus: false, |
81 | | - }); |
| 78 | + infoWindow.setContent(content); |
| 79 | + infoWindow.setPosition(center); |
| 80 | + infoWindow.open({ |
| 81 | + map: innerMap, |
| 82 | + anchor: marker, |
| 83 | + shouldFocus: false, |
| 84 | + }); |
82 | 85 | } |
83 | 86 |
|
84 | 87 | initMap(); |
|
0 commit comments