|
8 | 8 | /* [START maps_ui_kit_place_details_query_selector] */ |
9 | 9 | const map = document.querySelector('gmp-map') as any; |
10 | 10 | const placeDetails = document.querySelector('gmp-place-details') as any; |
| 11 | +const placeDetailsRequest = document.querySelector('gmp-place-details-place-request') as any; |
11 | 12 | const marker = document.querySelector('gmp-advanced-marker') as any; |
12 | 13 | /* [END maps_ui_kit_place_details_query_selector] */ |
13 | 14 |
|
14 | 15 | let center = { lat: 47.759737, lng: -122.250632 }; |
15 | 16 |
|
16 | 17 | async function initMap(): Promise<void> { |
17 | 18 | // Request needed libraries. |
18 | | - const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary; |
19 | | - const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; |
20 | | - const { Place } = await google.maps.importLibrary("places") as google.maps.PlacesLibrary; |
| 19 | + await google.maps.importLibrary("maps") as google.maps.MapsLibrary; |
| 20 | + await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; |
| 21 | + await google.maps.importLibrary("places") as google.maps.PlacesLibrary; |
21 | 22 |
|
22 | 23 | // Hide the map type control. |
23 | 24 | map.innerMap.setOptions({mapTypeControl: false}); |
24 | 25 |
|
25 | | - // Set the default selection. |
26 | | - const place = new Place({ |
27 | | - id: "ChIJC8HakaIRkFQRiOgkgdHmqkk", |
28 | | - requestedLanguage: "en", // optional |
| 26 | + // Set up map once widget is loaded. |
| 27 | + placeDetails.addEventListener('gmp-load', (event) => { |
| 28 | + map.innerMap.panTo(placeDetails.place.location); |
| 29 | + map.innerMap.setZoom(16); // Set zoom after panning if needed |
| 30 | + marker.position = placeDetails.place.location; |
| 31 | + marker.style.display = 'block'; |
| 32 | + marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; |
29 | 33 | }); |
30 | | - await placeDetails.configureFromPlace(place); |
31 | | - let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005); |
32 | | - map.innerMap.panTo(adjustedCenter); |
33 | | - map.innerMap.setZoom(16); |
34 | | - marker.position = placeDetails.place.location; |
35 | | - marker.style.display = 'block'; |
36 | | - marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; |
37 | 34 |
|
38 | 35 | /* [START maps_ui_kit_place_details_event] */ |
39 | | - // Add an event listener to handle map clicks. |
| 36 | + // Add an event listener to handle clicks. |
40 | 37 | map.innerMap.addListener('click', async (event) => { |
41 | 38 | marker.position = null; |
42 | 39 | event.stop(); |
43 | 40 | if (event.placeId) { |
44 | 41 | // Fire when the user clicks a POI. |
45 | | - await placeDetails.configureFromPlace({id: event.placeId}); |
| 42 | + placeDetailsRequest.place = event.placeId; |
46 | 43 | } else { |
47 | 44 | // Fire when the user clicks the map (not on a POI). |
48 | | - await placeDetails.configureFromLocation(event.latLng); |
| 45 | + console.log('No place was selected.'); |
49 | 46 | } |
50 | | - // Get the offset center. |
51 | | - let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005); |
52 | 47 |
|
53 | 48 | // Show the marker at the selected place. |
54 | 49 | marker.position = placeDetails.place.location; |
55 | 50 | marker.style.display = 'block'; |
56 | | - map.innerMap.panTo(adjustedCenter); |
| 51 | + marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; |
57 | 52 | }); |
58 | 53 | } |
59 | 54 | /* [END maps_ui_kit_place_details_event] */ |
60 | 55 |
|
61 | | -// Helper function to offset the map center. |
62 | | -function offsetLatLngRight(latLng, longitudeOffset) { |
63 | | - const newLng = latLng.lng() + longitudeOffset; |
64 | | - return new google.maps.LatLng(latLng.lat(), newLng); |
65 | | -} |
66 | | - |
67 | 56 | initMap(); |
68 | 57 | /* [END maps_ui_kit_place_details] */ |
0 commit comments