|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +// [START maps_geocoding_reverse] |
| 7 | +let marker; |
| 8 | + |
| 9 | +async function initMap() { |
| 10 | + // Request the needed libraries. |
| 11 | + const [{ Map, InfoWindow }, { Geocoder }, { AdvancedMarkerElement }] = |
| 12 | + await Promise.all([ |
| 13 | + google.maps.importLibrary( |
| 14 | + 'maps' |
| 15 | + ) as Promise<google.maps.MapsLibrary>, |
| 16 | + google.maps.importLibrary( |
| 17 | + 'geocoding' |
| 18 | + ) as Promise<google.maps.GeocodingLibrary>, |
| 19 | + google.maps.importLibrary( |
| 20 | + 'marker' |
| 21 | + ) as Promise<google.maps.MarkerLibrary>, |
| 22 | + ]); |
| 23 | + |
| 24 | + // Get the gmp-map element. |
| 25 | + const mapElement = document.querySelector( |
| 26 | + 'gmp-map' |
| 27 | + ) as google.maps.MapElement; |
| 28 | + |
| 29 | + // Get the inner map. |
| 30 | + const innerMap = mapElement.innerMap; |
| 31 | + |
| 32 | + // Get the latlng input box. |
| 33 | + const latLngQuery = document.getElementById('latlng') as HTMLInputElement; |
| 34 | + |
| 35 | + // Get the submit button. |
| 36 | + const submitButton = document.getElementById('submit') as HTMLElement; |
| 37 | + |
| 38 | + // Set the cursor to crosshair. |
| 39 | + innerMap.setOptions({ |
| 40 | + draggableCursor: 'crosshair', |
| 41 | + zoom: 13, |
| 42 | + }); |
| 43 | + |
| 44 | + // Create a marker for re-use. |
| 45 | + marker = new AdvancedMarkerElement({ |
| 46 | + map: innerMap, |
| 47 | + }); |
| 48 | + |
| 49 | + const geocoder = new Geocoder(); |
| 50 | + const infowindow = new InfoWindow(); |
| 51 | + |
| 52 | + // Add a click event listener to the submit button. |
| 53 | + submitButton.addEventListener('click', () => { |
| 54 | + geocodeLatLng(geocoder, innerMap, infowindow); |
| 55 | + }); |
| 56 | + |
| 57 | + // Add a click event listener to the map. |
| 58 | + innerMap.addListener('click', (event) => { |
| 59 | + latLngQuery.value = `${event.latLng.lat()}, ${event.latLng.lng()}`; |
| 60 | + geocodeLatLng(geocoder, innerMap, infowindow); |
| 61 | + }); |
| 62 | + |
| 63 | + // Make an initial request upon loading. |
| 64 | + geocodeLatLng(geocoder, innerMap, infowindow); |
| 65 | +} |
| 66 | + |
| 67 | +async function geocodeLatLng( |
| 68 | + geocoder: google.maps.Geocoder, |
| 69 | + map: google.maps.Map, |
| 70 | + infowindow: google.maps.InfoWindow |
| 71 | +) { |
| 72 | + const input = (document.getElementById('latlng') as HTMLInputElement).value; |
| 73 | + const latlngStr = input.split(',', 2); |
| 74 | + const latlng = { |
| 75 | + lat: parseFloat(latlngStr[0]), |
| 76 | + lng: parseFloat(latlngStr[1]), |
| 77 | + }; |
| 78 | + |
| 79 | + geocoder |
| 80 | + .geocode({ location: latlng }) |
| 81 | + .then((response) => { |
| 82 | + if (response.results[0]) { |
| 83 | + marker.position = latlng; |
| 84 | + map.setCenter(latlng); |
| 85 | + infowindow.setContent(response.results[0].formatted_address); |
| 86 | + infowindow.open(map, marker); |
| 87 | + } else { |
| 88 | + window.alert('No results found'); |
| 89 | + } |
| 90 | + }) |
| 91 | + .catch((e) => window.alert('Geocoder failed due to: ' + e)); |
| 92 | +} |
| 93 | + |
| 94 | +initMap(); |
| 95 | +// [END maps_geocoding_reverse] |
0 commit comments