6
6
7
7
// [START maps_place_text_search]
8
8
let map ;
9
- let center ;
9
+ let markers = { } ;
10
+ let infoWindow ;
10
11
11
12
async function initMap ( ) {
12
- const { Map } = await google . maps . importLibrary ( "maps" ) as google . maps . MapsLibrary ;
13
+ const { Map, InfoWindow } = await google . maps . importLibrary ( "maps" ) as google . maps . MapsLibrary ;
13
14
14
- center = { lat : 37.4161493 , lng : - 122.0812166 } ;
15
+ const center = { lat : 37.4161493 , lng : - 122.0812166 } ;
15
16
map = new Map ( document . getElementById ( 'map' ) as HTMLElement , {
16
17
center : center ,
17
18
zoom : 11 ,
19
+ mapTypeControl : false ,
18
20
mapId : 'DEMO_MAP_ID' ,
19
21
} ) ;
20
22
21
- findPlaces ( ) ;
23
+ const textInput = document . getElementById ( 'text-input' ) as HTMLInputElement ;
24
+ const textInputButton = document . getElementById ( 'text-input-button' ) as HTMLButtonElement ;
25
+ const card = document . getElementById ( 'text-input-card' ) as HTMLElement ;
26
+ map . controls [ google . maps . ControlPosition . TOP_LEFT ] . push ( card ) ;
27
+
28
+ textInputButton . addEventListener ( 'click' , ( ) => {
29
+ findPlaces ( textInput . value ) ;
30
+ } ) ;
31
+
32
+ textInput . addEventListener ( 'keydown' , ( event ) => {
33
+ if ( event . key === 'Enter' ) {
34
+ findPlaces ( textInput . value ) ;
35
+ }
36
+ } ) ;
37
+
38
+ infoWindow = new google . maps . InfoWindow ( ) ;
22
39
}
23
40
24
- async function findPlaces ( ) {
41
+ async function findPlaces ( query ) {
25
42
const { Place } = await google . maps . importLibrary ( "places" ) as google . maps . PlacesLibrary ;
26
43
const { AdvancedMarkerElement } = await google . maps . importLibrary ( "marker" ) as google . maps . MarkerLibrary ;
27
44
// [START maps_place_text_search_request]
28
45
const request = {
29
- textQuery : 'Tacos in Mountain View' ,
46
+ textQuery : query ,
30
47
fields : [ 'displayName' , 'location' , 'businessStatus' ] ,
31
- includedType : 'restaurant' ,
32
- locationBias : { lat : 37.4161493 , lng : - 122.0812166 } ,
48
+ includedType : '' , // Restrict query to a specific type (leave blank for any).
49
+ useStrictTypeFiltering : true ,
50
+ locationBias : map . center ,
33
51
isOpenNow : true ,
34
52
language : 'en-US' ,
35
53
maxResultCount : 8 ,
36
- minRating : 3.2 ,
54
+ minRating : 1 , // Specify a minimum rating.
37
55
region : 'us' ,
38
- useStrictTypeFiltering : false ,
39
56
} ;
40
57
41
- //@ts -ignore
42
58
const { places } = await Place . searchByText ( request ) ;
43
59
// [END maps_place_text_search_request]
44
60
45
61
if ( places . length ) {
46
- console . log ( places ) ;
47
-
48
62
const { LatLngBounds } = await google . maps . importLibrary ( "core" ) as google . maps . CoreLibrary ;
49
63
const bounds = new LatLngBounds ( ) ;
64
+
65
+ // First remove all existing markers.
66
+ for ( const id in markers ) {
67
+ markers [ id ] . map = null ;
68
+ } ;
69
+ markers = { } ;
50
70
51
71
// Loop through and get all the results.
52
- places . forEach ( ( place ) => {
53
- const markerView = new AdvancedMarkerElement ( {
72
+ places . forEach ( place => {
73
+ const marker = new AdvancedMarkerElement ( {
54
74
map,
55
75
position : place . location ,
56
76
title : place . displayName ,
57
77
} ) ;
78
+ markers [ place . id ] = marker ;
79
+
80
+ marker . addListener ( 'gmp-click' , ( ) => {
81
+ map . panTo ( place . location ) ;
82
+ updateInfoWindow ( place . displayName , place . id , marker ) ;
83
+ } ) ;
58
84
59
- bounds . extend ( place . location as google . maps . LatLng ) ;
60
- console . log ( place ) ;
85
+ if ( place . location != null ) {
86
+ bounds . extend ( place . location ) ;
87
+ }
61
88
} ) ;
62
89
63
90
map . fitBounds ( bounds ) ;
@@ -67,5 +94,16 @@ async function findPlaces() {
67
94
}
68
95
}
69
96
97
+ // Helper function to create an info window.
98
+ async function updateInfoWindow ( title , content , anchor ) {
99
+ infoWindow . setContent ( content ) ;
100
+ infoWindow . setHeaderContent ( title ) ;
101
+ infoWindow . open ( {
102
+ map,
103
+ anchor,
104
+ shouldFocus : false ,
105
+ } ) ;
106
+ }
107
+
70
108
initMap ( ) ;
71
109
// [END maps_place_text_search]
0 commit comments