Skip to content

Commit fb27413

Browse files
authored
Add 3d-clamp-mode sample (#920)
* Add 3d-clamp-mode sample * Update README.md
1 parent 36a557c commit fb27413

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

samples/3d-clamp-mode/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Google Maps JavaScript Sample
2+
3+
## 3d-clamp-mode
4+
5+
## Setup
6+
7+
The 3d-clamp-mode sample demonstrates how different clamp modes impact how a polyline is rendered on the map.
8+
9+
Follow these instructions to set up and run 3d-clamp-mode sample on your local computer.
10+
11+
### Before starting run:
12+
13+
`$npm i`
14+
15+
### Run an example on a local web server
16+
17+
First `cd` to the folder for the sample to run, then:
18+
19+
`$npm start`
20+
21+
### Build an individual example
22+
23+
From `samples/`:
24+
25+
`$npm run build --workspace=3d-clamp-mode/`
26+
27+
### Build all of the examples.
28+
29+
From `samples/`:
30+
`$npm run build-all`
31+
32+
## Feedback
33+
34+
For feedback related to this sample, please open a new issue on
35+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
36+

samples/3d-clamp-mode/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2025 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_3d_clamp_mode] -->
8+
<html>
9+
<head>
10+
<title>Map</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
</head>
15+
<body>
16+
<div id="map"></div>
17+
18+
<!-- prettier-ignore -->
19+
<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))})
20+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "beta",});</script>
21+
22+
<selector>
23+
<select id="selectElementId">
24+
<option value="CLAMP_TO_GROUND" selected>Clamp to Ground</option>
25+
<option value="RELATIVE_TO_GROUND">Relative to Ground</option>
26+
<option value="RELATIVE_TO_MESH">Relative to Mesh</option>
27+
<option value="ABSOLUTE">Absolute</option>
28+
</select>
29+
</selector>
30+
</body>
31+
</html>
32+
<!-- [END maps_3d_clamp_mode] -->

samples/3d-clamp-mode/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
//@ts-nocheck
8+
// [START maps_3d_clamp_mode]
9+
let polyline;
10+
11+
async function init() {
12+
const { Map3DElement, AltitudeMode, Polyline3DElement } = await google.maps.importLibrary("maps3d");
13+
14+
const map = new Map3DElement({
15+
center: { lat: 47.6205, lng: -122.3493, altitude: 300 },
16+
tilt: 67.5,
17+
range: 2000,
18+
heading: 0,
19+
mode: 'SATELLITE',
20+
gestureHandling: "COOPERATIVE"
21+
});
22+
23+
// Create an east-west polyline through the Space Needle
24+
polyline = new Polyline3DElement({
25+
path: [
26+
{ lat: 47.6205, lng: -122.3593, altitude: 100 }, // West point
27+
{ lat: 47.6205, lng: -122.3393, altitude: 100 } // East point
28+
],
29+
strokeColor: 'red',
30+
strokeWidth: 5,
31+
altitudeMode: AltitudeMode.CLAMP_TO_GROUND,
32+
});
33+
34+
map.append(polyline);
35+
document.body.append(map);
36+
}
37+
38+
init();
39+
40+
// Dropdown event listener
41+
const dropdown = document.getElementById('selectElementId');
42+
dropdown.addEventListener('change', updateAltitudeMode);
43+
44+
function updateAltitudeMode(event) {
45+
if (polyline && dropdown.value) {
46+
polyline.altitudeMode = dropdown.value;
47+
}
48+
}
49+
50+
// [END maps_3d_clamp_mode]

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_3d_clamp_mode] */
7+
/* * Always set the map height explicitly to define the size of the div element
8+
* that contains the map.
9+
*/
10+
html,
11+
map {
12+
height: 100%;
13+
}
14+
body {
15+
height: 100%;
16+
margin: 0;
17+
padding: 0;
18+
}
19+
selector {
20+
padding: 2px;
21+
float: right;
22+
}
23+
/* [END maps_3d_clamp_mode] */
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)