Skip to content

Commit d340d28

Browse files
authored
feat: Migrates the circle-simple sample. (#1098)
* feat: Migrates the circle-simple sample. * Add comment for control panel display property Added comment to clarify display property usage.
1 parent 46ee0d5 commit d340d28

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

samples/circle-simple/README.md

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

samples/circle-simple/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_circle_simple] -->
8+
<html>
9+
<head>
10+
<title>Circles</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
20+
center="34.98956821576194, 135.74239981260283"
21+
zoom="15"
22+
map-id="DEMO_MAP_ID">
23+
<div id="control-panel" slot="control-inline-start-block-start">
24+
<input
25+
id="short-walk"
26+
type="radio"
27+
name="radius"
28+
value="400"
29+
checked />
30+
<label for="short-walk">Short Walk (~5 minutes)</label><br />
31+
<input
32+
id="medium-walk"
33+
type="radio"
34+
name="radius"
35+
value="800" />
36+
<label for="medium-walk">Medium Walk (~15 minutes)</label><br />
37+
<input id="long-walk" type="radio" name="radius" value="1600" />
38+
<label for="long-walk">Long Walk (~30 minutes) </label>
39+
</div>
40+
</gmp-map>
41+
</body>
42+
</html>
43+
<!-- [END maps_circle_simple] -->

samples/circle-simple/index.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_circle_simple]
8+
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
9+
let innerMap;
10+
11+
async function initMap() {
12+
// Import the needed libraries.
13+
// Request needed libraries.
14+
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;
15+
(await google.maps.importLibrary('marker')) as google.maps.MarkerLibrary;
16+
// Get the gmp-map element.
17+
const mapElement = document.querySelector(
18+
'gmp-map'
19+
) as google.maps.MapElement;
20+
21+
const initialCenter = { lat: 34.98956821576194, lng: 135.74239981260283 }; // Hotel Emion, Kyoto, Japan
22+
23+
// Get the inner map.
24+
const innerMap = mapElement.innerMap;
25+
26+
const buttons = document.querySelectorAll('input[name="radius"]');
27+
28+
const walkingCircle = new google.maps.Circle({
29+
strokeColor: '#ffdd00ff',
30+
strokeOpacity: 0.8,
31+
strokeWeight: 2,
32+
fillColor: '#ffdd00ff',
33+
fillOpacity: 0.35,
34+
map: innerMap,
35+
center: initialCenter,
36+
radius: 400,
37+
draggable: true,
38+
editable: false,
39+
});
40+
41+
// Define a "Crosshair" vector icon
42+
const parser = new DOMParser();
43+
const svgString = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-6 -6 12 12"><path d="M -6,0 L 6,0 M 0,-6 L 0,6" stroke="black" stroke-width="1"/></svg>`;
44+
45+
const pinSvg = parser.parseFromString(
46+
svgString,
47+
'image/svg+xml'
48+
).documentElement;
49+
50+
const centerMarker = new google.maps.marker.AdvancedMarkerElement({
51+
position: initialCenter,
52+
title: 'A marker using a custom SVG image.',
53+
//@ts-ignore
54+
anchorLeft: '-50%',
55+
anchorTop: '-50%',
56+
});
57+
centerMarker.append(pinSvg);
58+
mapElement.append(centerMarker);
59+
60+
// Wait for the map to finish drawing its tiles.
61+
google.maps.event.addListenerOnce(innerMap, 'tilesloaded', function () {
62+
// Get the controls div
63+
const controls = document.getElementById('control-panel');
64+
65+
// Display controls once map is loaded.
66+
if (controls) {
67+
controls.style.display = 'block';
68+
}
69+
});
70+
71+
// Add event listener to update the radius based on user selection.
72+
buttons.forEach((button) => {
73+
button.addEventListener('change', (event) => {
74+
const target = event.target as HTMLInputElement;
75+
walkingCircle.setRadius(Number(target.value));
76+
});
77+
});
78+
79+
// Handle user click, reset the map center and position the circle.
80+
innerMap.addListener('click', (mapsMouseEvent) => {
81+
const newCenter = mapsMouseEvent.latLng;
82+
walkingCircle.setCenter(newCenter);
83+
centerMarker.position = newCenter;
84+
innerMap.panTo(newCenter);
85+
});
86+
87+
// Handle user dragging the circle, update the center marker position.
88+
walkingCircle.addListener('center_changed', () => {
89+
centerMarker.position = walkingCircle.getCenter();
90+
});
91+
}
92+
93+
initMap();
94+
// [END maps_circle_simple]

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_circle_simple] */
7+
/*
8+
* Optional: Makes the sample page fill the window.
9+
*/
10+
html,
11+
body {
12+
height: 100%;
13+
margin: 0;
14+
padding: 0;
15+
}
16+
17+
#control-panel {
18+
display: none; /* Set to 'display: block' after the map loads. */
19+
background-color: #fff;
20+
border: 2px solid #fff;
21+
border-radius: 3px;
22+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
23+
font-family: "Roboto", "sans-serif";
24+
font-size: medium;
25+
margin: 10px;
26+
padding: 10px;
27+
}
28+
/* [END maps_circle_simple] */
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)