Skip to content

Commit 7393a07

Browse files
committed
feat: Migrates the control-custom-state sample.
1 parent 064ffff commit 7393a07

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed
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+
## control-custom-state
4+
5+
This example demonstrates adding controls that store state.
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/control-custom-state`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/control-custom-state`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=control-custom-state/`
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/control-custom-state`
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).
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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_control_custom_state] -->
8+
<html>
9+
<head>
10+
<title>Adding State to Controls</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="41.85, -87.65" zoom="12">
20+
<div
21+
class="custom-control"
22+
slot="control-block-start-inline-center">
23+
<input
24+
type="button"
25+
class="button"
26+
id="btnCenterMap"
27+
value="Center Map"
28+
title="Click to recenter the map." />
29+
<input
30+
type="button"
31+
class="button"
32+
id="btnSetCenter"
33+
value="Set Center"
34+
title="Click to change the center of the map." />
35+
</div>
36+
</gmp-map>
37+
</body>
38+
</html>
39+
<!-- [END maps_control_custom_state] -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_control_custom_state]
8+
let innerMap;
9+
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
10+
11+
let center: google.maps.LatLngLiteral = { lat: 41.85, lng: -87.65 };
12+
13+
async function initMap() {
14+
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;
15+
16+
innerMap = mapElement.innerMap;
17+
18+
// Get the button UI elements.
19+
const setCenterButton = document.getElementById('btnCenterMap') as HTMLInputElement;
20+
const resetCenterButton = document.getElementById('btnSetCenter') as HTMLInputElement;
21+
22+
// [START maps_control_custom_state_event_listeners]
23+
// Set up the click event listener for the 'Center Map' button. Set the map
24+
// to the currently stored center.
25+
setCenterButton.addEventListener('click', () => {
26+
const currentCenter = center;
27+
innerMap.setCenter(currentCenter);
28+
});
29+
30+
// Set up the click event listener for 'Set Center': Set the center of
31+
// the control to the current center of the map.
32+
resetCenterButton.addEventListener('click', () => {
33+
const newCenter = innerMap.getCenter();
34+
35+
if (newCenter) {
36+
center = newCenter;
37+
}
38+
});
39+
// [END maps_control_custom_state_event_listeners]
40+
}
41+
42+
initMap();
43+
// [END maps_control_custom_state]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/control-custom-state",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh control-custom-state && bash ../app.sh control-custom-state && bash ../docs.sh control-custom-state && npm run build:vite --workspace=. && bash ../dist.sh control-custom-state",
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_control_custom_state] */
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+
#btnCenterMap,
19+
#btnSetCenter {
20+
background-color: #fff;
21+
border: 2px solid #fff;
22+
border-radius: 3px;
23+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
24+
cursor: pointer;
25+
float: left;
26+
margin-bottom: 22px;
27+
margin-top: 5px;
28+
text-align: center;
29+
color: rgb(25, 25, 25);
30+
font-family: Roboto, Arial, sans-serif;
31+
font-size: 15px;
32+
line-height: 25px;
33+
}
34+
35+
#btnSetCenter {
36+
margin-left: 12px;
37+
}
38+
39+
/* [END maps_control_custom_state] */
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)