|
| 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] |
0 commit comments