@@ -27,22 +27,15 @@ let infoWindow: google.maps.InfoWindow;
2727// The init function is called when the page loads.
2828async function init ( ) : Promise < void > {
2929 // Import the necessary libraries from the Google Maps API.
30- const [ { InfoWindow } , { Place } , markerLib , coreLib ] = await Promise . all ( [
30+ const [ { InfoWindow } , { Place } ] = await Promise . all ( [
3131 google . maps . importLibrary ( 'maps' ) as Promise < google . maps . MapsLibrary > ,
3232 google . maps . importLibrary (
3333 'places'
3434 ) as Promise < google . maps . PlacesLibrary > ,
35- google . maps . importLibrary (
36- 'marker'
37- ) as Promise < google . maps . MarkerLibrary > ,
38- google . maps . importLibrary ( 'core' ) as Promise < google . maps . CoreLibrary > ,
3935 ] ) ;
4036
41- const AdvancedMarkerElement = markerLib . AdvancedMarkerElement ;
42-
43- const LatLngBounds = coreLib . LatLngBounds ;
44-
4537 // Create a new info window and set its content to the place details element.
38+ placeDetails . remove ( ) ; // Hide the place details element because it is not needed until the info window opens
4639 infoWindow = new InfoWindow ( {
4740 content : placeDetails ,
4841 ariaLabel : 'Place Details' ,
@@ -55,33 +48,26 @@ async function init(): Promise<void> {
5548 streetViewControl : false ,
5649 } ) ;
5750
58- // Add a click listener to the map to hide the info window when the map is clicked.
59- map . innerMap . addListener ( 'click' , ( ) => infoWindow . close ( ) ) ;
6051 /* [START maps_ui_kit_place_search_nearby_event] */
6152 // Add event listeners to the type select and place search elements.
62- typeSelect . addEventListener ( 'change' , ( ) =>
63- searchPlaces ( AdvancedMarkerElement , LatLngBounds )
64- ) ;
53+ typeSelect . addEventListener ( 'change' , ( ) => searchPlaces ( ) ) ;
6554
6655 placeSearch . addEventListener ( 'gmp-select' , ( event : Event ) => {
6756 const { place } = event as any ;
6857 markers . get ( place . id ) ?. click ( ) ;
6958 if ( place . location ) {
70- map . innerMap . setCenter ( place . location ) ;
59+ map . center = place . location ;
7160 }
7261 } ) ;
7362 placeSearch . addEventListener ( 'gmp-load' , ( ) => {
74- addMarkers ( AdvancedMarkerElement , LatLngBounds ) ;
63+ addMarkers ( ) ;
7564 } ) ;
7665
77- searchPlaces ( AdvancedMarkerElement , LatLngBounds ) ;
66+ searchPlaces ( ) ;
7867}
7968/* [END maps_ui_kit_place_search_nearby_event] */
8069// The searchPlaces function is called when the user changes the type select or when the page loads.
81- function searchPlaces (
82- AdvancedMarkerElement : typeof google . maps . marker . AdvancedMarkerElement ,
83- LatLngBounds : typeof google . maps . LatLngBounds
84- ) {
70+ async function searchPlaces ( ) {
8571 // Close the info window and clear the markers.
8672 infoWindow . close ( ) ;
8773 for ( const marker of markers . values ( ) ) {
@@ -91,23 +77,27 @@ function searchPlaces(
9177
9278 // Set the place search query and add an event listener to the place search element.
9379 if ( typeSelect . value ) {
94- const center = map . innerMap . getCenter ( ) ! ;
80+ const center = map . center ! ;
9581 placeSearchQuery . locationRestriction = {
96- center : { lat : center . lat ( ) , lng : center . lng ( ) } ,
82+ center,
9783 radius : 50000 , // 50km radius
9884 } ;
9985 placeSearchQuery . locationBias = {
100- center : { lat : center . lat ( ) , lng : center . lng ( ) } ,
86+ center,
10187 } ;
10288 placeSearchQuery . includedTypes = [ typeSelect . value ] ;
10389 }
10490}
10591
10692// The addMarkers function is called when the place search element loads.
107- async function addMarkers (
108- AdvancedMarkerElement : typeof google . maps . marker . AdvancedMarkerElement ,
109- LatLngBounds : typeof google . maps . LatLngBounds
110- ) {
93+ async function addMarkers ( ) {
94+ // Import the necessary libraries from the Google Maps API.
95+ const [ { AdvancedMarkerElement } , { LatLngBounds } ] = await Promise . all ( [
96+ google . maps . importLibrary (
97+ 'marker'
98+ ) as Promise < google . maps . MarkerLibrary > ,
99+ google . maps . importLibrary ( 'core' ) as Promise < google . maps . CoreLibrary > ,
100+ ] ) ;
111101 const bounds = new LatLngBounds ( ) ;
112102
113103 if ( placeSearch . places . length === 0 ) {
0 commit comments