|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +// [START maps_3d_camera_position] |
| 7 | +async function initMap(): Promise<void> { |
| 8 | + // Declare the needed libraries. |
| 9 | + await google.maps.importLibrary('maps3d'); |
| 10 | + |
| 11 | + const map3DElement = document.querySelector('gmp-map-3d')!; |
| 12 | + |
| 13 | + // Elements from HTML |
| 14 | + const headingSlider = document.getElementById( |
| 15 | + 'heading' |
| 16 | + ) as HTMLInputElement; |
| 17 | + const tiltSlider = document.getElementById('tilt') as HTMLInputElement; |
| 18 | + const rangeSlider = document.getElementById('range') as HTMLInputElement; |
| 19 | + const latSlider = document.getElementById('lat') as HTMLInputElement; |
| 20 | + const lngSlider = document.getElementById('lng') as HTMLInputElement; |
| 21 | + const fovSlider = document.getElementById('fov') as HTMLInputElement; |
| 22 | + const rollSlider = document.getElementById('roll') as HTMLInputElement; |
| 23 | + |
| 24 | + const headingVal = document.getElementById('heading-val') as HTMLElement; |
| 25 | + const tiltVal = document.getElementById('tilt-val') as HTMLElement; |
| 26 | + const rangeVal = document.getElementById('range-val') as HTMLElement; |
| 27 | + const altitudeVal = document.getElementById('altitude-val') as HTMLElement; |
| 28 | + const fovVal = document.getElementById('fov-val') as HTMLElement; |
| 29 | + const rollVal = document.getElementById('roll-val') as HTMLElement; |
| 30 | + const codeElem = document.getElementById('generated-code') as HTMLElement; |
| 31 | + const copyBtn = document.getElementById('copy-btn') as HTMLButtonElement; |
| 32 | + |
| 33 | + let currentAltitude = 30; |
| 34 | + let isUserInteracting = false; |
| 35 | + |
| 36 | + // Update values on UI when the map changes. |
| 37 | + const updateUI = () => { |
| 38 | + const heading = map3DElement.heading?.toFixed(0) ?? '0'; |
| 39 | + const tilt = map3DElement.tilt?.toFixed(0) ?? '0'; |
| 40 | + const range = map3DElement.range?.toFixed(0) ?? '0'; |
| 41 | + const rawFov = parseFloat(map3DElement.fov?.toFixed(0) ?? '45'); |
| 42 | + const fovClamped = Math.min(80, Math.max(5, rawFov)); |
| 43 | + const fov = fovClamped.toString(); |
| 44 | + const roll = map3DElement.roll?.toFixed(0) ?? '0'; |
| 45 | + const center = map3DElement.center; |
| 46 | + const mode = map3DElement.mode; |
| 47 | + |
| 48 | + headingVal.textContent = heading; |
| 49 | + tiltVal.textContent = tilt; |
| 50 | + rangeVal.textContent = range; |
| 51 | + fovVal.textContent = fov; |
| 52 | + rollVal.textContent = roll; |
| 53 | + |
| 54 | + if (!isUserInteracting) { |
| 55 | + fovSlider.value = fov; |
| 56 | + headingSlider.value = heading; |
| 57 | + tiltSlider.value = tilt; |
| 58 | + rangeSlider.value = Math.min(10000, parseFloat(range)).toString(); |
| 59 | + rollSlider.value = roll; |
| 60 | + } |
| 61 | + |
| 62 | + if (center) { |
| 63 | + const lat = center.lat.toFixed(4); |
| 64 | + const lng = center.lng.toFixed(4); |
| 65 | + const alt = currentAltitude.toFixed(0); |
| 66 | + |
| 67 | + latSlider.value = lat; |
| 68 | + lngSlider.value = lng; |
| 69 | + altitudeVal.textContent = alt; |
| 70 | + |
| 71 | + codeElem.textContent = `<gmp-map-3d center="${lat},${lng},${alt}" mode="${mode}" tilt="${tilt}" range="${range}" heading="${heading}" fov="${fov}" roll="${roll}"></gmp-map-3d>`; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Copy generated HTML to clipboard. |
| 76 | + copyBtn.addEventListener('click', () => { |
| 77 | + void navigator.clipboard.writeText(codeElem.textContent || ''); |
| 78 | + copyBtn.textContent = 'Copied!'; |
| 79 | + setTimeout(() => { |
| 80 | + copyBtn.textContent = 'Copy HTML'; |
| 81 | + }, 2000); |
| 82 | + }); |
| 83 | + |
| 84 | + // Listen to slider changes using event delegation. |
| 85 | + const panel = document.querySelector('.panel') as HTMLElement; |
| 86 | + |
| 87 | + panel.addEventListener('input', (e) => { |
| 88 | + const target = e.target as HTMLInputElement; |
| 89 | + if (target.tagName !== 'INPUT') return; |
| 90 | + |
| 91 | + isUserInteracting = true; |
| 92 | + const prop = target.name; |
| 93 | + const val = parseFloat(target.value); |
| 94 | + |
| 95 | + if (prop === 'lat') { |
| 96 | + const currentCenter = map3DElement.center; |
| 97 | + if (currentCenter) { |
| 98 | + map3DElement.center = { |
| 99 | + lat: val, |
| 100 | + lng: currentCenter.lng, |
| 101 | + altitude: currentCenter.altitude, |
| 102 | + }; |
| 103 | + } |
| 104 | + } else if (prop === 'lng') { |
| 105 | + const currentCenter = map3DElement.center; |
| 106 | + if (currentCenter) { |
| 107 | + map3DElement.center = { |
| 108 | + lat: currentCenter.lat, |
| 109 | + lng: val, |
| 110 | + altitude: currentCenter.altitude, |
| 111 | + }; |
| 112 | + } |
| 113 | + } else if (prop === 'altitude') { |
| 114 | + currentAltitude = val; |
| 115 | + const currentCenter = map3DElement.center; |
| 116 | + if (currentCenter) { |
| 117 | + map3DElement.center = { |
| 118 | + lat: currentCenter.lat, |
| 119 | + lng: currentCenter.lng, |
| 120 | + altitude: val, |
| 121 | + }; |
| 122 | + } |
| 123 | + } else { |
| 124 | + map3DElement[prop] = val; |
| 125 | + } |
| 126 | + updateUI(); |
| 127 | + }); |
| 128 | + |
| 129 | + panel.addEventListener('change', (e) => { |
| 130 | + const target = e.target as HTMLInputElement; |
| 131 | + if (target.tagName === 'INPUT') { |
| 132 | + isUserInteracting = false; |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + // Update UI on camera change events. |
| 137 | + map3DElement.addEventListener('gmp-headingchange', updateUI); |
| 138 | + map3DElement.addEventListener('gmp-tiltchange', updateUI); |
| 139 | + map3DElement.addEventListener('gmp-rangechange', updateUI); |
| 140 | + map3DElement.addEventListener('gmp-fovchange', updateUI); |
| 141 | + |
| 142 | + // Initial UI sync |
| 143 | + setTimeout(updateUI, 500); |
| 144 | +} |
| 145 | + |
| 146 | +void initMap(); |
| 147 | +// [END maps_3d_camera_position] |
0 commit comments