Skip to content

Commit b43513f

Browse files
committed
Added simple map and places samples for 3d
1 parent 9d9239e commit b43513f

File tree

10 files changed

+243
-0
lines changed

10 files changed

+243
-0
lines changed

samples/3d-places/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Map</title>
5+
6+
<link rel="stylesheet" type="text/css" href="./style.css" />
7+
<script type="module" src="./index.js"></script>
8+
</head>
9+
<body>
10+
<div id="map"></div>
11+
12+
<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))})
13+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha",});</script>
14+
</body>
15+
</html>

samples/3d-places/index.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* * https://www.apache.org/licenses/LICENSE-2.0
8+
* * Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
// [START maps3d_places]
16+
let map3DElement = null;
17+
async function init() {
18+
const { Map3DElement, MapMode } = await google.maps.importLibrary("maps3d");
19+
map3DElement = new Map3DElement({
20+
center: {lat: 0, lng: 0, altitude: 16000000},
21+
mode: MapMode.HYBRID,
22+
});
23+
document.body.append(map3DElement);
24+
initAutocomplete();
25+
}
26+
async function initAutocomplete() {
27+
const { Autocomplete } = await google.maps.importLibrary("places");
28+
const autocomplete = new Autocomplete(
29+
document.getElementById("pac-input"),
30+
{
31+
fields: [
32+
"geometry",
33+
"name",
34+
"place_id"
35+
],
36+
}
37+
);
38+
autocomplete.addListener("place_changed", () => {
39+
//viewer.entities.removeAll();
40+
const place = autocomplete.getPlace();
41+
if (!place.geometry || !place.geometry.viewport) {
42+
window.alert("No viewport for input: " + place.name);
43+
return;
44+
}
45+
zoomToViewport(place.geometry);
46+
});
47+
}
48+
const zoomToViewport = async (geometry) => {
49+
const { AltitudeMode, Polyline3DElement } = await google.maps.importLibrary("maps3d");
50+
let viewport = geometry.viewport;
51+
let locationPoints = [
52+
{ lat: viewport.getNorthEast().lat(), lng: viewport.getNorthEast().lng() },
53+
{ lat: viewport.getSouthWest().lat(), lng: viewport.getNorthEast().lng() },
54+
{ lat: viewport.getSouthWest().lat(), lng: viewport.getSouthWest().lng() },
55+
{ lat: viewport.getNorthEast().lat(), lng: viewport.getSouthWest().lng() },
56+
{ lat: viewport.getNorthEast().lat(), lng: viewport.getNorthEast().lng() }
57+
];
58+
let locationPolyline = new Polyline3DElement({
59+
altitudeMode: AltitudeMode.CLAMP_TO_GROUND,
60+
strokeColor: "blue",
61+
strokeWidth: 10,
62+
coordinates: locationPoints,
63+
});
64+
map3DElement.append(locationPolyline);
65+
console.log(locationPolyline);
66+
let elevation = await getElevationforPoint(geometry.location);
67+
if (map3DElement) {
68+
map3DElement.center = { lat: geometry.location.lat(), lng: geometry.location.lng(), altitude: elevation + 50 };
69+
map3DElement.heading = 0;
70+
map3DElement.range = 1000;
71+
map3DElement.tilt = 65;
72+
}
73+
};
74+
async function getElevationforPoint(location) {
75+
const { ElevationService } = await google.maps.importLibrary("elevation");
76+
// Get place elevation using the ElevationService.
77+
const elevatorService = new google.maps.ElevationService();
78+
const elevationResponse = await elevatorService.getElevationForLocations({
79+
locations: [location],
80+
});
81+
if (!(elevationResponse.results && elevationResponse.results.length)) {
82+
window.alert(`Insufficient elevation data for place: ${place.name}`);
83+
return;
84+
}
85+
const elevation = elevationResponse.results[0].elevation || 10;
86+
return elevation;
87+
}
88+
init();
89+
// [END maps3d_places]

samples/3d-places/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/3d-places",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh 3d-places && bash ../app.sh 3d-places && bash ../docs.sh 3d-places && npm run build:vite --workspace=. && bash ../dist.sh 3d-places",
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-places/style.css

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* * https://www.apache.org/licenses/LICENSE-2.0
8+
* * Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
/* [START 3d_places] */
15+
/* * Always set the map height explicitly to define the size of the div element
16+
* that contains the map.
17+
*/
18+
#map {
19+
height: 100%;
20+
}
21+
22+
/* [END 3d_places] */

samples/3d-places/tsconfig.json

+17
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+
}

samples/3d-simple-map/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Simple Map</title>
5+
6+
<link rel="stylesheet" type="text/css" href="./style.css" />
7+
<script type="module" src="./index.js"></script>
8+
</head>
9+
<body>
10+
<gmp-map-3d mode="hybrid" center="37.7704,-122.3985,500" tilt="67.5"></gmp-map-3d>
11+
12+
<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))})
13+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha",});</script>
14+
</body>
15+
</html>

samples/3d-simple-map/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* * https://www.apache.org/licenses/LICENSE-2.0
8+
* * Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
//@ts-nocheck
16+
// [START maps3d_simple_map]
17+
18+
// [END maps3d_simple_map]

samples/3d-simple-map/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/3d-simple-map",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh 3d-simple-map && bash ../app.sh 3d-simple-map && bash ../docs.sh 3d-simple-map && npm run build:vite --workspace=. && bash ../dist.sh 3d-simple-map",
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-simple-map/style.css

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* * https://www.apache.org/licenses/LICENSE-2.0
8+
* * Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
/* [START 3d_simple_map] */
15+
/* * Always set the map height explicitly to define the size of the div element
16+
* that contains the map.
17+
*/
18+
#gmp-map-3d {
19+
height: 100%;
20+
}
21+
22+
/* [END 3d_simple_map] */

samples/3d-simple-map/tsconfig.json

+17
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)