Skip to content

Commit cc093ba

Browse files
authored
feat: Merges the event-closure sample to js-api-samples. (#1262)
1 parent cfa7603 commit cc093ba

6 files changed

Lines changed: 177 additions & 0 deletions

File tree

samples/event-closure/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Google Maps JavaScript Sample
2+
3+
## event-closure
4+
5+
This sample demonstrates using closures with event listeners.
6+
7+
## Setup
8+
9+
### Before starting run:
10+
11+
`npm i`
12+
13+
### Run an example on a local web server
14+
15+
`cd samples/event-closure`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/event-closure`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=event-closure/`
26+
27+
### Build all of the examples.
28+
29+
From 'samples':
30+
31+
`npm run build-all`
32+
33+
### Run lint to check for problems
34+
35+
`cd samples/event-closure`
36+
`npx eslint index.ts`
37+
38+
## Feedback
39+
40+
For feedback related to this sample, please open a new issue on
41+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).

samples/event-closure/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2026 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_event_closure] -->
8+
<html>
9+
<head>
10+
<title>Using Closures in Event Listeners</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
16+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
17+
</head>
18+
<body>
19+
<gmp-map center="-25.363882, 131.044922" zoom="4" map-id="DEMO_MAP_ID"></gmp-map>
20+
</body>
21+
</html>
22+
<!-- [END maps_event_closure] -->

samples/event-closure/index.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_event_closure]
8+
async function initMap() {
9+
// Request needed libraries.
10+
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;
11+
(await google.maps.importLibrary('marker')) as google.maps.MarkerLibrary;
12+
13+
const mapElement = document.querySelector(
14+
'gmp-map'
15+
) as google.maps.MapElement;
16+
const innerMap = mapElement.innerMap;
17+
18+
const bounds: google.maps.LatLngBoundsLiteral = {
19+
north: -25.363882,
20+
south: -31.203405,
21+
east: 131.044922,
22+
west: 125.244141,
23+
};
24+
25+
// Display the area between the location southWest and northEast.
26+
innerMap.fitBounds(bounds);
27+
28+
// Add 5 markers to map at random locations.
29+
// For each of these markers, give them a title with their index, and when
30+
// they are clicked they should open an infowindow with text from a secret
31+
// message.
32+
const secretMessages = ['This', 'is', 'the', 'secret', 'message'];
33+
const lngSpan = bounds.east - bounds.west;
34+
const latSpan = bounds.north - bounds.south;
35+
36+
for (let i = 0; i < secretMessages.length; ++i) {
37+
const marker = new google.maps.marker.AdvancedMarkerElement({
38+
position: {
39+
lat: bounds.south + latSpan * Math.random(),
40+
lng: bounds.west + lngSpan * Math.random(),
41+
},
42+
map: innerMap,
43+
});
44+
45+
attachSecretMessage(marker, secretMessages[i]);
46+
}
47+
}
48+
49+
// Attaches an info window to a marker with the provided message. When the
50+
// marker is clicked, the info window will open with the secret message.
51+
function attachSecretMessage(
52+
marker: google.maps.marker.AdvancedMarkerElement,
53+
secretMessage: string
54+
) {
55+
const infowindow = new google.maps.InfoWindow({
56+
content: secretMessage,
57+
});
58+
59+
marker.addListener('gmp-click', () => {
60+
infowindow.open(marker.map, marker);
61+
});
62+
}
63+
64+
initMap();
65+
// [END maps_event_closure]

samples/event-closure/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/event-closure",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh event-closure && bash ../app.sh event-closure && bash ../docs.sh event-closure && npm run build:vite --workspace=. && bash ../dist.sh event-closure",
6+
"test": "tsc && npm run build:vite --workspace=.",
7+
"start": "tsc && vite build --base './' && vite",
8+
"build:vite": "vite build --base './'",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
13+
}
14+
}

samples/event-closure/style.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_event_closure] */
7+
8+
/*
9+
* Optional: Makes the sample page fill the window.
10+
*/
11+
html,
12+
body {
13+
height: 100%;
14+
margin: 0;
15+
padding: 0;
16+
}
17+
18+
/* [END maps_event_closure] */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"strict": true,
6+
"noImplicitAny": false,
7+
"lib": [
8+
"es2015",
9+
"esnext",
10+
"es6",
11+
"dom",
12+
"dom.iterable"
13+
],
14+
"moduleResolution": "Node",
15+
"jsx": "preserve"
16+
}
17+
}

0 commit comments

Comments
 (0)