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_search_text] */
7+ /* [START maps_ui_kit_place_search_text_query_selectors] */
8+ const map = document . querySelector ( "gmp-map" ) as any ;
9+ const placeList = document . querySelector ( "gmp-place-list" ) as any ;
10+ const placeDetails = document . querySelector ( "gmp-place-details" ) as any ;
11+ let marker = document . querySelector ( 'gmp-advanced-marker' ) as any ;
12+ /* [END maps_ui_kit_place_search_text_query_selectors] */
13+ /* [START maps_ui_kit_place_search_text_init_map] */
14+ let markers = { } ;
15+ let infoWindow ;
16+ let center = { lat : 37.395641 , lng : - 122.077627 } ;
17+
18+ async function initMap ( ) : Promise < void > {
19+ await google . maps . importLibrary ( "places" ) ;
20+ const { InfoWindow } = await google . maps . importLibrary ( "maps" ) as google . maps . MapsLibrary ;
21+ const { Place } = await google . maps . importLibrary ( "places" ) as google . maps . PlacesLibrary ;
22+
23+ infoWindow = new google . maps . InfoWindow ;
24+
25+ // Center the map
26+ let adjustedCenter = offsetLatLngRight ( center , - 0.005 ) ;
27+ map . innerMap . panTo ( adjustedCenter ) ;
28+ map . innerMap . setZoom ( 14 ) ;
29+
30+ map . innerMap . setOptions ( {
31+ mapTypeControl : false ,
32+ clickableIcons : false ,
33+ } ) ;
34+
35+ searchByTextRequest ( 'tacos near me' ) ;
36+ }
37+ /* [END maps_ui_kit_place_search_text_init_map] */
38+
39+ /* [START maps_ui_kit_place_search_text_query] */
40+ async function searchByTextRequest ( textQuery ) {
41+ if ( textQuery ) {
42+ placeList . configureFromSearchByTextRequest ( {
43+ locationRestriction : map . innerMap . getBounds ( ) ,
44+ includedType : "restaurant" ,
45+ textQuery : textQuery ,
46+ } ) . then ( addMarkers ) ;
47+ // Handle user selection in Place Details.
48+ placeList . addEventListener ( "gmp-placeselect" , ( { place } ) => {
49+ markers [ place . id ] . click ( ) ;
50+ } ) ;
51+ }
52+ }
53+ /* [END maps_ui_kit_place_search_text_query] */
54+ /* [START maps_ui_kit_place_search_text_add_markers] */
55+ async function addMarkers ( ) {
56+ const { AdvancedMarkerElement } = await google . maps . importLibrary ( "marker" ) as google . maps . MarkerLibrary ;
57+ const { LatLngBounds } = await google . maps . importLibrary ( "core" ) as google . maps . CoreLibrary ;
58+
59+ const bounds = new LatLngBounds ( ) ;
60+
61+ if ( placeList . places . length > 0 ) {
62+ placeList . places . forEach ( ( place ) => {
63+ let marker = new AdvancedMarkerElement ( {
64+ map : map . innerMap ,
65+ position : place . location ,
66+ } ) ;
67+
68+ markers [ place . id ] = marker ;
69+ bounds . extend ( place . location ) ;
70+ marker . collisionBehavior = google . maps . CollisionBehavior . REQUIRED_AND_HIDES_OPTIONAL ;
71+
72+ marker . addListener ( 'gmp-click' , ( event ) => {
73+ if ( infoWindow . isOpen ) {
74+ infoWindow . close ( ) ;
75+ }
76+ placeDetails . configureFromPlace ( place ) ;
77+ placeDetails . style . width = "350px" ;
78+ infoWindow . setOptions ( {
79+ content : placeDetails
80+ } ) ;
81+ infoWindow . open ( {
82+ anchor : marker ,
83+ map : map . innerMap
84+ } ) ;
85+ placeDetails . addEventListener ( 'gmp-load' , ( ) => {
86+ map . innerMap . fitBounds ( place . viewport , { top : 400 , left : 400 } ) ;
87+ } ) ;
88+
89+ } ) ;
90+ map . innerMap . setCenter ( bounds . getCenter ( ) ) ;
91+ map . innerMap . fitBounds ( bounds ) ;
92+ } ) ;
93+ }
94+ }
95+ /* [END maps_ui_kit_place_search_text_add_markers] */
96+
97+ // Helper function to offset the map center.
98+ function offsetLatLngRight ( latLng , longitudeOffset ) {
99+ const newLng = latLng . lng + longitudeOffset ;
100+ return new google . maps . LatLng ( latLng . lat , newLng ) ;
101+ }
102+
103+ declare global {
104+ interface Window {
105+ initMap : ( ) => void ;
106+ }
107+ }
108+ window . initMap = initMap ;
109+ /* [END maps_ui_kit_place_search_text] */
110+ export { } ;
0 commit comments