Skip to content

Commit d6274b5

Browse files
authored
feat: Migrates the rectangle-event sample to js-api-samples. (#1317)
1 parent 2d0d8ca commit d6274b5

6 files changed

Lines changed: 175 additions & 0 deletions

File tree

samples/rectangle-event/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+
## rectangle-event
4+
5+
This sample demonstrates how to listen to events on a rectangle in the Maps JavaScript API.
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/rectangle-event`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/rectangle-event`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=rectangle-event/`
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/rectangle-event`
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/rectangle-event/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_rectangle_event] -->
8+
<html>
9+
<head>
10+
<title>Listening to 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="44.5452,-78.5389" zoom="9" map-id="DEMO_MAP_ID"></gmp-map>
20+
</body>
21+
</html>
22+
<!-- [END maps_rectangle_event] -->

samples/rectangle-event/index.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_rectangle_event]
8+
// This example adds a user-editable rectangle to the map.
9+
// When the user changes the bounds of the rectangle,
10+
// an info window pops up displaying the new bounds.
11+
12+
let rectangle: google.maps.Rectangle;
13+
let innerMap: google.maps.Map;
14+
15+
let infoWindow: google.maps.InfoWindow;
16+
17+
async function initMap(): Promise<void> {
18+
await google.maps.importLibrary("maps");
19+
20+
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
21+
innerMap = mapElement.innerMap;
22+
23+
const bounds = {
24+
north: 44.599,
25+
south: 44.49,
26+
east: -78.443,
27+
west: -78.649,
28+
};
29+
30+
// Define the rectangle and set its editable property to true.
31+
rectangle = new google.maps.Rectangle({
32+
bounds: bounds,
33+
editable: true,
34+
draggable: true,
35+
});
36+
37+
rectangle.setMap(innerMap);
38+
39+
// Add an event listener on the rectangle.
40+
rectangle.addListener("bounds_changed", showNewRect);
41+
42+
// Define an info window on the map.
43+
infoWindow = new google.maps.InfoWindow();
44+
}
45+
46+
/** Show the new coordinates for the rectangle in an info window. */
47+
function showNewRect() {
48+
const ne = rectangle.getBounds()!.getNorthEast();
49+
const sw = rectangle.getBounds()!.getSouthWest();
50+
51+
const contentString =
52+
"<b>Rectangle moved.</b><br>" +
53+
"New north-east corner: " +
54+
ne.lat() +
55+
", " +
56+
ne.lng() +
57+
"<br>" +
58+
"New south-west corner: " +
59+
sw.lat() +
60+
", " +
61+
sw.lng();
62+
63+
// Set the info window's content and position.
64+
infoWindow.setContent(contentString);
65+
infoWindow.setPosition(ne);
66+
67+
infoWindow.open(innerMap);
68+
}
69+
70+
initMap();
71+
// [END maps_rectangle_event]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/rectangle-event",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh rectangle-event && bash ../app.sh rectangle-event && bash ../docs.sh rectangle-event && npm run build:vite --workspace=. && bash ../dist.sh rectangle-event",
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/rectangle-event/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_rectangle_event] */
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_rectangle_event] */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"rootDir": "."
5+
},
6+
"include": [
7+
"./*.ts",
8+
]
9+
}

0 commit comments

Comments
 (0)