77
88/* [START maps_ui_kit_place_search_nearby_query_selectors] */
99// Query selectors for various elements in the HTML file.
10- const map = document . querySelector ( 'gmp-map' ) as google . maps . MapElement
11- const placeSearch = document . querySelector ( 'gmp-place-search' ) as any
10+ const map = document . querySelector ( 'gmp-map' ) as google . maps . MapElement ;
11+ const placeSearch = document . querySelector ( 'gmp-place-search' ) as any ;
1212const placeSearchQuery = document . querySelector (
1313 'gmp-place-nearby-search-request'
14- ) as any
15- const placeDetails = document . querySelector ( 'gmp-place-details-compact' ) as any
14+ ) as any ;
15+ const placeDetails = document . querySelector ( 'gmp-place-details-compact' ) as any ;
1616const placeRequest = document . querySelector (
1717 'gmp-place-details-place-request'
18- ) as any
19- const typeSelect = document . querySelector ( '.type-select' ) as HTMLSelectElement
18+ ) as any ;
19+ const typeSelect = document . querySelector ( '.type-select' ) as HTMLSelectElement ;
2020/* [END maps_ui_kit_place_search_nearby_query_selectors] */
2121
2222// Global variables for the map, markers, and info window.
23- const markers : Map < string , google . maps . marker . AdvancedMarkerElement > = new Map ( )
24- let infoWindow : google . maps . InfoWindow
25- let AdvancedMarkerElement : typeof google . maps . marker . AdvancedMarkerElement
26- let LatLngBounds : typeof google . maps . LatLngBounds
23+ const markers : Map < string , google . maps . marker . AdvancedMarkerElement > =
24+ new Map ( ) ;
25+ let infoWindow : google . maps . InfoWindow ;
2726
2827// The init function is called when the page loads.
2928async function init ( ) : Promise < void > {
@@ -37,106 +36,103 @@ async function init(): Promise<void> {
3736 'marker'
3837 ) as Promise < google . maps . MarkerLibrary > ,
3938 google . maps . importLibrary ( 'core' ) as Promise < google . maps . CoreLibrary > ,
40- ] )
39+ ] ) ;
4140
42- AdvancedMarkerElement = markerLib . AdvancedMarkerElement
43- LatLngBounds = coreLib . LatLngBounds
41+ const AdvancedMarkerElement = markerLib . AdvancedMarkerElement ;
42+
43+ const LatLngBounds = coreLib . LatLngBounds ;
4444
4545 // Create a new info window and set its content to the place details element.
4646 infoWindow = new InfoWindow ( {
4747 content : placeDetails ,
4848 ariaLabel : 'Place Details' ,
49- headerDisabled : true ,
50- pixelOffset : new google . maps . Size ( 0 , - 40 ) ,
51- } )
49+ } ) ;
5250
5351 // Set the map options.
5452 map . innerMap . setOptions ( {
5553 clickableIcons : false ,
5654 mapTypeControl : false ,
5755 streetViewControl : false ,
58- } )
56+ } ) ;
5957
6058 // Add a click listener to the map to hide the info window when the map is clicked.
61- map . innerMap . addListener ( 'click' , ( ) => infoWindow . close ( ) )
59+ map . innerMap . addListener ( 'click' , ( ) => infoWindow . close ( ) ) ;
6260 /* [START maps_ui_kit_place_search_nearby_event] */
6361 // Add event listeners to the type select and place search elements.
64- typeSelect . addEventListener ( 'change' , searchPlaces )
62+ typeSelect . addEventListener ( 'change' , ( ) =>
63+ searchPlaces ( AdvancedMarkerElement , LatLngBounds )
64+ ) ;
6565
6666 placeSearch . addEventListener ( 'gmp-select' , ( event : Event ) => {
67- const { place } = event as any
68- if ( markers . has ( place . id ) ) {
69- markers . get ( place . id ) ! . click ( )
70- }
67+ const { place } = event as any ;
68+ markers . get ( place . id ) ?. click ( ) ;
7169 if ( place . location ) {
72- map . innerMap . setCenter ( place . location )
70+ map . innerMap . setCenter ( place . location ) ;
7371 }
74- } )
75- placeSearch . addEventListener (
76- 'gmp-load' ,
77- ( ) => {
78- searchPlaces ( )
79- } ,
80- { once : true }
81- )
72+ } ) ;
73+ placeSearch . addEventListener ( 'gmp-load' , ( ) => {
74+ searchPlaces ( AdvancedMarkerElement , LatLngBounds ) ;
75+ addMarkers ( AdvancedMarkerElement , LatLngBounds ) ;
76+ } ) ;
8277}
8378/* [END maps_ui_kit_place_search_nearby_event] */
8479// The searchPlaces function is called when the user changes the type select or when the page loads.
85- function searchPlaces ( ) {
80+ function searchPlaces (
81+ AdvancedMarkerElement : typeof google . maps . marker . AdvancedMarkerElement ,
82+ LatLngBounds : typeof google . maps . LatLngBounds
83+ ) {
8684 // Close the info window and clear the markers.
87- infoWindow . close ( )
85+ infoWindow . close ( ) ;
8886 for ( const marker of markers . values ( ) ) {
89- marker . map = null
87+ marker . remove ( ) ;
9088 }
91- markers . clear ( )
89+ markers . clear ( ) ;
9290
9391 // Set the place search query and add an event listener to the place search element.
9492 if ( typeSelect . value ) {
95- const center = map . innerMap . getCenter ( ) !
96- placeSearchQuery . maxResultCount = 5
93+ const center = map . innerMap . getCenter ( ) ! ;
94+ placeSearchQuery . maxResultCount = 5 ;
9795 placeSearchQuery . locationRestriction = {
9896 center : { lat : center . lat ( ) , lng : center . lng ( ) } ,
9997 radius : 50000 , // 50km radius
100- }
98+ } ;
10199 placeSearchQuery . locationBias = {
102100 center : { lat : center . lat ( ) , lng : center . lng ( ) } ,
103- }
104- placeSearchQuery . includedTypes = [ typeSelect . value ]
105- placeSearch . addEventListener ( 'gmp-load' , addMarkers , { once : true } )
101+ } ;
102+ placeSearchQuery . includedTypes = [ typeSelect . value ] ;
106103 }
107104}
108105
109106// The addMarkers function is called when the place search element loads.
110- async function addMarkers ( ) {
111- const bounds = new LatLngBounds ( )
107+ async function addMarkers (
108+ AdvancedMarkerElement : typeof google . maps . marker . AdvancedMarkerElement ,
109+ LatLngBounds : typeof google . maps . LatLngBounds
110+ ) {
111+ const bounds = new LatLngBounds ( ) ;
112112
113113 if ( placeSearch . places . length === 0 ) {
114- return
114+ return ;
115115 }
116116
117- placeSearch . places . forEach ( ( place ) => {
117+ for ( const place of placeSearch . places ) {
118118 const marker = new AdvancedMarkerElement ( {
119119 map : map . innerMap ,
120120 position : place . location ,
121121 collisionBehavior :
122122 google . maps . CollisionBehavior . REQUIRED_AND_HIDES_OPTIONAL ,
123- } )
123+ } ) ;
124124
125- markers . set ( place . id , marker )
126- bounds . extend ( place . location )
125+ markers . set ( place . id , marker ) ;
126+ bounds . extend ( place . location ) ;
127127
128128 marker . addListener ( 'click' , ( ) => {
129- if ( place . location ) {
130- map . innerMap . setCenter ( place . location )
131- }
132- placeRequest . place = place
133- infoWindow . setPosition ( place . location )
134- infoWindow . open ( map . innerMap )
135- } )
136- } )
137-
138- map . innerMap . fitBounds ( bounds )
129+ placeRequest . place = place ;
130+ infoWindow . open ( map . innerMap , marker ) ;
131+ } ) ;
132+ }
133+
134+ map . innerMap . fitBounds ( bounds ) ;
139135}
140136
141- init ( )
137+ init ( ) ;
142138/* [END maps_ui_kit_place_search_nearby] */
0 commit comments