-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (40 loc) · 1.22 KB
/
index.js
File metadata and controls
40 lines (40 loc) · 1.22 KB
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
"use strict";
/**
* @license
* Copyright 2026 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_geocoding_component_restriction]
async function initMap() {
await Promise.all([
google.maps.importLibrary("maps"),
google.maps.importLibrary("marker"),
google.maps.importLibrary("geocoding"),
]);
const geocoder = new google.maps.Geocoder();
const mapElement = document.querySelector('gmp-map');
const innerMap = mapElement.innerMap;
document.getElementById("submit").addEventListener("click", () => {
geocodeAddress(geocoder, innerMap);
});
}
function geocodeAddress(geocoder, map) {
geocoder
.geocode({
address: "483 George St.",
componentRestrictions: {
country: "AU",
postalCode: "2000",
},
})
.then(({ results }) => {
map.setCenter(results[0].geometry.location);
new google.maps.marker.AdvancedMarkerElement({
map,
position: results[0].geometry.location,
});
})
.catch((e) => window.alert("Geocode was not successful for the following reason: " + e));
}
initMap();
// [END maps_geocoding_component_restriction]