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