1+ /*
2+ * @license
3+ * Copyright 2025 Google LLC. All Rights Reserved.
4+ * SPDX-License-Identifier: Apache-2.0
5+ */
6+ /* [START maps_ui_kit_place_details] */
7+ // Use querySelector to select elements for interaction.
8+ /* [START maps_ui_kit_place_details_query_selector] */
9+ const map = document . querySelector ( 'gmp-map' ) as any ;
10+ const placeDetails = document . querySelector ( 'gmp-place-details' ) as any ;
11+ const marker = document . querySelector ( 'gmp-advanced-marker' ) as any ;
12+ /* [END maps_ui_kit_place_details_query_selector] */
13+
14+ async function initMap ( ) : Promise < void > {
15+ // Request needed libraries.
16+ const { Map } = await google . maps . importLibrary ( "maps" ) as google . maps . MapsLibrary ;
17+ const { AdvancedMarkerElement } = await google . maps . importLibrary ( "marker" ) as google . maps . MarkerLibrary ;
18+ const { Place } = await google . maps . importLibrary ( "places" ) as google . maps . PlacesLibrary ;
19+
20+ // Calls the geolocation helper function to center the map on the current
21+ // device location.
22+ findCurrentLocation ( ) ;
23+
24+ // Hide the map type control.
25+ map . innerMap . setOptions ( { mapTypeControl : false } ) ;
26+
27+ /* [START maps_ui_kit_place_details_event] */
28+ // Add an event listener to handle map clicks.
29+ map . innerMap . addListener ( 'click' , async ( event ) => {
30+ marker . position = null ;
31+ event . stop ( ) ;
32+ placeDetails . style . visibility = 'visible' ;
33+ if ( event . placeId ) {
34+ // Fire when the user clicks a POI.
35+ await placeDetails . configureFromPlace ( { id : event . placeId } ) ;
36+ } else {
37+ // Fire when the user clicks the map (not on a POI).
38+ await placeDetails . configureFromLocation ( event . latLng ) ;
39+ }
40+ // Show the marker at the selected place.
41+ marker . position = placeDetails . place . location ;
42+ marker . style . display = 'block' ;
43+ } ) ;
44+ }
45+ /* [END maps_ui_kit_place_details_event] */
46+
47+ // Helper function for geolocation.
48+ async function findCurrentLocation ( ) {
49+ const { LatLng } = await google . maps . importLibrary ( 'core' ) as google . maps . CoreLibrary ;
50+ if ( navigator . geolocation ) {
51+ navigator . geolocation . getCurrentPosition (
52+ ( position ) => {
53+ const pos =
54+ new LatLng ( position . coords . latitude , position . coords . longitude ) ;
55+ map . innerMap . panTo ( pos ) ;
56+ map . innerMap . setZoom ( 16 ) ;
57+ } ,
58+ ( ) => {
59+ console . log ( 'The Geolocation service failed.' ) ;
60+ map . innerMap . setZoom ( 16 ) ;
61+ } ,
62+ ) ;
63+ } else {
64+ console . log ( 'Your browser doesn\'t support geolocation' ) ;
65+ map . innerMap . setZoom ( 16 ) ;
66+ }
67+ }
68+
69+ declare global {
70+ interface Window {
71+ initMap : ( ) => void ;
72+ }
73+ }
74+ window . initMap = initMap ;
75+ /* [END maps_ui_kit_place_details] */
76+ export { } ;
0 commit comments