Skip to content

Commit 99970e1

Browse files
committed
feat: Migrates elevation-simple to js-api-samples.
1 parent 729feb2 commit 99970e1

6 files changed

Lines changed: 165 additions & 0 deletions

File tree

samples/elevation-simple/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+
## elevation-simple
4+
5+
Elevation Simple sample demonstrating the Elevation Service.
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/elevation-simple`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/elevation-simple`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=elevation-simple/`
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/elevation-simple`
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_elevation_simple] -->
8+
<html>
9+
<head>
10+
<title>Elevation Service</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
<script>
15+
// prettier-ignore
16+
(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))})({
17+
key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8"
18+
});
19+
</script>
20+
</head>
21+
<body>
22+
<gmp-map
23+
center="63.333,-150.5"
24+
zoom="8"
25+
map-type-id="terrain"></gmp-map>
26+
</body>
27+
</html>
28+
<!-- [END maps_elevation_simple] -->

samples/elevation-simple/index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_elevation_simple]
8+
async function initMap(): Promise<void> {
9+
// Request needed libraries.
10+
await google.maps.importLibrary('maps');
11+
await google.maps.importLibrary('elevation');
12+
13+
const mapElement = document.querySelector('gmp-map')!;
14+
const innerMap = mapElement.innerMap;
15+
16+
const elevator = new google.maps.ElevationService();
17+
const infowindow = new google.maps.InfoWindow({});
18+
19+
infowindow.open(innerMap);
20+
21+
// Add a listener for the click event. Display the elevation for the LatLng of
22+
// the click inside the infowindow.
23+
innerMap.addListener('click', (event: google.maps.MapMouseEvent) => {
24+
displayLocationElevation(event.latLng!, elevator, infowindow);
25+
});
26+
}
27+
28+
function displayLocationElevation(
29+
location: google.maps.LatLng,
30+
elevator: google.maps.ElevationService,
31+
infowindow: google.maps.InfoWindow
32+
) {
33+
// Initiate the location request
34+
elevator
35+
.getElevationForLocations({
36+
locations: [location],
37+
})
38+
.then(({ results }) => {
39+
infowindow.setPosition(location);
40+
41+
// Retrieve the first result
42+
if (results[0]) {
43+
// Open the infowindow indicating the elevation at the clicked position.
44+
infowindow.setContent(
45+
'The elevation at this point <br>is ' +
46+
results[0].elevation +
47+
' meters.'
48+
);
49+
} else {
50+
infowindow.setContent('No results found');
51+
}
52+
})
53+
.catch((e) =>
54+
infowindow.setContent('Elevation service failed due to: ' + e)
55+
);
56+
}
57+
58+
void initMap();
59+
// [END maps_elevation_simple]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@js-api-samples/elevation-simple",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh elevation-simple && bash ../app.sh elevation-simple && bash ../docs.sh elevation-simple && npm run build:vite --workspace=. && bash ../dist.sh elevation-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+
}

samples/elevation-simple/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_elevation_simple] */
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_elevation_simple] */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"rootDir": "."
5+
},
6+
"include": ["./*.ts"]
7+
}

0 commit comments

Comments
 (0)