Skip to content

Commit d62f33b

Browse files
authored
feat: Migrates the map-events sample to js-api-samples. (#1258)
1 parent 09cb653 commit d62f33b

6 files changed

Lines changed: 212 additions & 0 deletions

File tree

samples/map-events/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+
## map-events
4+
5+
This sample shows which events are triggered by the google.maps.Map as you interact with the map.
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/map-events`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/map-events`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=map-events/`
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/map-events`
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/map-events/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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_map_events] -->
8+
<html>
9+
<head>
10+
<title>Map Events</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="37.4419, -122.1419" zoom="13">
20+
<div id="container" slot="control-inline-start-block-start">
21+
<div id="sidebar"></div>
22+
</div>
23+
</gmp-map>
24+
</body>
25+
</html>
26+
<!-- [END maps_map_events] -->

samples/map-events/index.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_map_event]
8+
const events = [
9+
'bounds_changed',
10+
'center_changed',
11+
'click',
12+
'contextmenu',
13+
'dblclick',
14+
'drag',
15+
'dragend',
16+
'dragstart',
17+
'heading_changed',
18+
'idle',
19+
'maptypeid_changed',
20+
'mousemove',
21+
'mouseout',
22+
'mouseover',
23+
'projection_changed',
24+
'resize',
25+
'rightclick', // use contextmenu
26+
'tilesloaded',
27+
'tilt_changed',
28+
'zoom_changed',
29+
];
30+
31+
function setupListener(map: google.maps.Map, name: string) {
32+
const eventRow = document.getElementById(name) as HTMLElement;
33+
google.maps.event.addListener(map, name, () => {
34+
eventRow.className = 'event active';
35+
const timeout = setTimeout(() => {
36+
eventRow.className = 'event inactive';
37+
}, 1000);
38+
});
39+
}
40+
41+
async function initMap() {
42+
// Request needed libraries.
43+
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;
44+
45+
const mapElement = document.querySelector(
46+
'gmp-map'
47+
) as google.maps.MapElement;
48+
49+
populateTable();
50+
51+
// Get the inner map.
52+
let innerMap = mapElement.innerMap;
53+
innerMap.setOptions({
54+
mapTypeControl: false,
55+
});
56+
57+
for (let i = 0; i < events.length; i++) {
58+
setupListener(innerMap, events[i]);
59+
}
60+
}
61+
62+
// Dynamically create the table of events from the defined hashmap
63+
function populateTable() {
64+
const eventsTable = document.getElementById('sidebar') as HTMLElement;
65+
66+
for (let i = 0; i < events.length; i++) {
67+
const eventDiv = document.createElement('div');
68+
eventDiv.className = 'event';
69+
eventDiv.id = events[i];
70+
eventDiv.innerText = events[i];
71+
eventsTable.appendChild(eventDiv);
72+
}
73+
}
74+
75+
initMap();
76+
// [END maps_map_event]

samples/map-events/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/map-events",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh map-events && bash ../app.sh map-events && bash ../docs.sh map-events && npm run build:vite --workspace=. && bash ../dist.sh map-events",
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/map-events/style.css

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_map_events] */
7+
/* Optional: Makes the sample page fill the window. */
8+
html,
9+
body {
10+
height: 100%;
11+
margin: 0;
12+
padding: 0;
13+
}
14+
15+
#container {
16+
height: 100%;
17+
}
18+
19+
#sidebar {
20+
width: 280px;
21+
padding: 2px;
22+
margin-left: 10px;
23+
margin-top: 10px;
24+
overflow: hidden;
25+
font-size: 15px;
26+
font-family: 'Droid Sans Mono', monospace;
27+
max-width: 300px;
28+
background-color: white;
29+
}
30+
31+
.active {
32+
background-color: #99ccff;
33+
}
34+
35+
.inactive {
36+
background-color: white;
37+
}
38+
/* [END maps_map_events] */

samples/map-events/tsconfig.json

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)