88
99const chicago = { lat : 41.85 , lng : - 87.65 } ;
1010
11- /**
12- * Creates a control that recenters the map on Chicago.
13- */
14- function createCenterControl ( map ) {
15- const controlButton = document . createElement ( 'button' ) ;
16-
17- // Set CSS for the control.
18- controlButton . style . backgroundColor = '#fff' ;
19- controlButton . style . border = '2px solid #fff' ;
20- controlButton . style . borderRadius = '3px' ;
21- controlButton . style . boxShadow = '0 2px 6px rgba(0,0,0,.3)' ;
22- controlButton . style . color = 'rgb(25,25,25)' ;
23- controlButton . style . cursor = 'pointer' ;
24- controlButton . style . fontFamily = 'Roboto,Arial,sans-serif' ;
25- controlButton . style . fontSize = '16px' ;
26- controlButton . style . lineHeight = '38px' ;
27- controlButton . style . margin = '8px 0 22px' ;
28- controlButton . style . padding = '0 5px' ;
29- controlButton . style . textAlign = 'center' ;
30-
31- controlButton . textContent = 'Center Map' ;
32- controlButton . title = 'Click to recenter the map' ;
33- controlButton . type = 'button' ;
34-
35- // Setup the click event listeners: simply set the map to Chicago.
36- controlButton . addEventListener ( 'click' , ( ) => {
37- map . setCenter ( chicago ) ;
38- } ) ;
39-
40- return controlButton ;
41- }
42-
4311//Get the gmp-map element
4412const mapElement = document . querySelector ( 'gmp-map' ) as google . maps . MapElement ;
4513
@@ -51,20 +19,12 @@ async function initMap() {
5119 // Get the inner map.
5220 const innerMap = mapElement . innerMap ;
5321
54- // Set map options.
55- mapElement . innerMap . setOptions ( {
56- zoom : 12 ,
57- center : chicago ,
58- } ) ;
59-
60- // Create the DIV to hold the control.
61- const centerControlDiv = document . createElement ( 'div' ) ;
62- // Create the control.
63- const centerControl = createCenterControl ( map ) ;
64- // Append the control to the DIV.
65- centerControlDiv . appendChild ( centerControl ) ;
66-
67- map . controls [ google . maps . ControlPosition . TOP_CENTER ] . push ( centerControlDiv ) ;
22+ // Get the button element from the HTML
23+ const centerButton = document . getElementById ( 'btnCenterMap' ) as HTMLButtonElement ;
24+ // Move the event listener here
25+ centerButton . addEventListener ( 'click' , ( ) => {
26+ innerMap . setCenter ( chicago ) ;
27+ } ) ;
6828}
6929
7030initMap ( ) ;
0 commit comments