-
Notifications
You must be signed in to change notification settings - Fork 7
Refine UI Kit Place Details Compact sample #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,74 +5,72 @@ | |||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||
| /* [START maps_ui_kit_place_details_compact] */ | ||||||||||||||||||||||||||||||||||||||||||||||
| // Use querySelector to select elements for interaction. | ||||||||||||||||||||||||||||||||||||||||||||||
| const mapContainer = document.getElementById('map-container') as any; | ||||||||||||||||||||||||||||||||||||||||||||||
| /* [START maps_ui_kit_place_details_compact_query_selector] */ | ||||||||||||||||||||||||||||||||||||||||||||||
| const map = document.querySelector('gmp-map') as google.maps.MapElement; | ||||||||||||||||||||||||||||||||||||||||||||||
| const placeDetails = document.querySelector('gmp-place-details-compact') as any; | ||||||||||||||||||||||||||||||||||||||||||||||
| const placeDetailsRequest = document.querySelector( | ||||||||||||||||||||||||||||||||||||||||||||||
| 'gmp-place-details-place-request' | ||||||||||||||||||||||||||||||||||||||||||||||
| ) as any; | ||||||||||||||||||||||||||||||||||||||||||||||
| let gMap; | ||||||||||||||||||||||||||||||||||||||||||||||
| let marker; | ||||||||||||||||||||||||||||||||||||||||||||||
| const marker = document.querySelector( | ||||||||||||||||||||||||||||||||||||||||||||||
| 'gmp-advanced-marker' | ||||||||||||||||||||||||||||||||||||||||||||||
| ) as google.maps.marker.AdvancedMarkerElement; | ||||||||||||||||||||||||||||||||||||||||||||||
| /* [END maps_ui_kit_place_details_compact_query_selector] */ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| async function initMap(): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||
| const { PlaceDetailsCompactElement, PlaceDetailsPlaceRequestElement } = | ||||||||||||||||||||||||||||||||||||||||||||||
| (await google.maps.importLibrary('places')) as any; | ||||||||||||||||||||||||||||||||||||||||||||||
| const { Map } = (await google.maps.importLibrary( | ||||||||||||||||||||||||||||||||||||||||||||||
| 'maps' | ||||||||||||||||||||||||||||||||||||||||||||||
| )) as google.maps.MapsLibrary; | ||||||||||||||||||||||||||||||||||||||||||||||
| // Request needed libraries. | ||||||||||||||||||||||||||||||||||||||||||||||
| await Promise.all([ | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we can tweak this to avoid strictly blocking on all these things up front |
||||||||||||||||||||||||||||||||||||||||||||||
| google.maps.importLibrary('maps'), | ||||||||||||||||||||||||||||||||||||||||||||||
| google.maps.importLibrary('marker'), | ||||||||||||||||||||||||||||||||||||||||||||||
| google.maps.importLibrary('places'), | ||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const { AdvancedMarkerElement } = (await google.maps.importLibrary( | ||||||||||||||||||||||||||||||||||||||||||||||
| 'marker' | ||||||||||||||||||||||||||||||||||||||||||||||
| )) as google.maps.MarkerLibrary; | ||||||||||||||||||||||||||||||||||||||||||||||
| gMap = new Map(mapContainer, { mapId: 'DEMO_MAP_ID' }); | ||||||||||||||||||||||||||||||||||||||||||||||
| marker = new AdvancedMarkerElement({ map: gMap }); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Set the map options. | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| map.innerMap.setOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||
| mapTypeControl: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| streetViewControl: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Set up the info window. | ||||||||||||||||||||||||||||||||||||||||||||||
| const infoWindow = new google.maps.InfoWindow({}); | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Hide the map type control. | ||||||||||||||||||||||||||||||||||||||||||||||
| gMap.setOptions({ mapTypeControl: false }); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Function to update map and marker based on place details | ||||||||||||||||||||||||||||||||||||||||||||||
| const updateMapAndMarker = () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (placeDetails.place && placeDetails.place.location) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const placeViewport = placeDetails.place.viewport; | ||||||||||||||||||||||||||||||||||||||||||||||
| map.innerMap.fitBounds(placeViewport, 0); | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.position = placeDetails.place.location; | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.collisionBehavior = | ||||||||||||||||||||||||||||||||||||||||||||||
| google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.style.display = 'block'; | ||||||||||||||||||||||||||||||||||||||||||||||
| infoWindow.close(); | ||||||||||||||||||||||||||||||||||||||||||||||
| infoWindow.setContent(placeDetails); | ||||||||||||||||||||||||||||||||||||||||||||||
| infoWindow.open({ map: map.innerMap, anchor: marker }); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and then auto-reformat for indentation |
||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Set up map, marker, and infowindow once widget is loaded. | ||||||||||||||||||||||||||||||||||||||||||||||
| placeDetails.style.visibility = 'visible'; | ||||||||||||||||||||||||||||||||||||||||||||||
| // Set up map once widget is loaded. | ||||||||||||||||||||||||||||||||||||||||||||||
| placeDetails.addEventListener('gmp-load', (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log('placeDetails initialized!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| updateMapAndMarker(); | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /* [START maps_ui_kit_place_details_compact_event] */ | ||||||||||||||||||||||||||||||||||||||||||||||
| // Add an event listener to handle clicks. | ||||||||||||||||||||||||||||||||||||||||||||||
| gMap.addListener('click', async (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| map.innerMap.addListener('click', async (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.position = null; | ||||||||||||||||||||||||||||||||||||||||||||||
| event.stop(); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Fire when the user clicks on a POI. | ||||||||||||||||||||||||||||||||||||||||||||||
| if (event.placeId) { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log('clicked on POI'); | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log(event.placeId); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Fire when the user clicks a POI. | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| placeDetailsRequest.place = event.placeId; | ||||||||||||||||||||||||||||||||||||||||||||||
| updateMapAndMarker(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think remove this here, and just let gmp-load on the details view trigger it? |
||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| // Fire when the user clicks the map (not on a POI). | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| console.log('No place was selected.'); | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.style.display = 'none'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| infoWindow.close(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
should happen automatically because it's attached to the marker |
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| // Function to update map, marker, and infowindow based on place details | ||||||||||||||||||||||||||||||||||||||||||||||
| const updateMapAndMarker = () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log('function called'); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (placeDetails.place && placeDetails.place.location) { | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.gMap = null; | ||||||||||||||||||||||||||||||||||||||||||||||
| let adjustedCenter = offsetLatLngRight( | ||||||||||||||||||||||||||||||||||||||||||||||
| placeDetails.place.location, | ||||||||||||||||||||||||||||||||||||||||||||||
| 0.002 | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| gMap.panTo(adjustedCenter); | ||||||||||||||||||||||||||||||||||||||||||||||
| gMap.setZoom(16); // Set zoom after panning if needed | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.content = placeDetails; | ||||||||||||||||||||||||||||||||||||||||||||||
| marker.position = placeDetails.place.location; | ||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log('else'); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| // Helper function to offset marker placement for better visual appearance. | ||||||||||||||||||||||||||||||||||||||||||||||
| function offsetLatLngRight(latLng, latitudeOffset) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const newLat = latLng.lat() + latitudeOffset; | ||||||||||||||||||||||||||||||||||||||||||||||
| return new google.maps.LatLng(newLat, latLng.lng()); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| /* [END maps_ui_kit_place_details_compact_event] */ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| initMap(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /* [END maps_ui_kit_place_details_compact] */ | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest leaving off the center and zoom, as the fitBounds() call later will cover that (and that would avoid a transition post-load)