|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_geocoding_simple] |
| 8 | +let geocoder: google.maps.Geocoder; |
| 9 | +let mapElement; |
| 10 | +let innerMap; |
| 11 | +let marker; |
| 12 | +let responseDiv; |
| 13 | +let response; |
| 14 | + |
| 15 | +async function initMap() { |
| 16 | + // Request the needed libraries. |
| 17 | + const [{ Map, InfoWindow }, { Geocoder }, { AdvancedMarkerElement }] = |
| 18 | + await Promise.all([ |
| 19 | + google.maps.importLibrary( |
| 20 | + 'maps' |
| 21 | + ) as Promise<google.maps.MapsLibrary>, |
| 22 | + google.maps.importLibrary( |
| 23 | + 'geocoding' |
| 24 | + ) as Promise<google.maps.GeocodingLibrary>, |
| 25 | + google.maps.importLibrary( |
| 26 | + 'marker' |
| 27 | + ) as Promise<google.maps.MarkerLibrary>, |
| 28 | + ]); |
| 29 | + |
| 30 | + // Get the gmp-map element. |
| 31 | + mapElement = document.querySelector( |
| 32 | + 'gmp-map' |
| 33 | + ) as google.maps.MapElement; |
| 34 | + |
| 35 | + // Get the inner map. |
| 36 | + innerMap = mapElement.innerMap; |
| 37 | + |
| 38 | + // Set the cursor to crosshair. |
| 39 | + innerMap.setOptions({ |
| 40 | + mapTypeControl: false, |
| 41 | + fullscreenControl: false, |
| 42 | + cameraControlOptions: { |
| 43 | + position: google.maps.ControlPosition.INLINE_START_BLOCK_END, |
| 44 | + }, |
| 45 | + draggableCursor: 'crosshair', |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + geocoder = new google.maps.Geocoder(); |
| 50 | + |
| 51 | + const inputText = document.getElementById('address') as HTMLInputElement; |
| 52 | + const submitButton = document.getElementById('submit') as HTMLInputElement; |
| 53 | + const clearButton = document.getElementById('clear') as HTMLInputElement; |
| 54 | + responseDiv = document.getElementById('response-container') as HTMLDivElement; |
| 55 | + response = document.getElementById('response') as HTMLPreElement; |
| 56 | + |
| 57 | + marker = new google.maps.marker.AdvancedMarkerElement({}); |
| 58 | + |
| 59 | + innerMap.addListener('click', (e: google.maps.MapMouseEvent) => { |
| 60 | + geocode({ location: e.latLng }); |
| 61 | + }); |
| 62 | + |
| 63 | + submitButton.addEventListener('click', () => |
| 64 | + geocode({ address: inputText.value }) |
| 65 | + ); |
| 66 | + |
| 67 | + clearButton.addEventListener('click', () => { |
| 68 | + clear(); |
| 69 | + }); |
| 70 | + |
| 71 | + clear(); |
| 72 | +} |
| 73 | + |
| 74 | +async function clear() { |
| 75 | + marker.setMap(null); |
| 76 | + responseDiv.style.display = 'none'; |
| 77 | +} |
| 78 | + |
| 79 | +async function geocode(request: google.maps.GeocoderRequest) { |
| 80 | + clear(); |
| 81 | + |
| 82 | + geocoder |
| 83 | + .geocode(request) |
| 84 | + .then((result) => { |
| 85 | + const { results } = result; |
| 86 | + innerMap.setCenter(results[0].geometry.location); |
| 87 | + marker.position = new google.maps.LatLng(results[0].geometry.location); |
| 88 | + mapElement.append(marker); |
| 89 | + responseDiv.style.display = 'block'; |
| 90 | + response.innerText = JSON.stringify(result, null, 2); |
| 91 | + return results; |
| 92 | + }) |
| 93 | + .catch((e) => { |
| 94 | + alert('Geocode was not successful for the following reason: ' + e); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +initMap(); |
| 99 | +// [END maps_geocoding_simple] |
0 commit comments