-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (47 loc) · 1.54 KB
/
index.js
File metadata and controls
47 lines (47 loc) · 1.54 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
41
42
43
44
45
46
47
"use strict";
/**
* @license
* Copyright 2026 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_geocoding_place_id]
// Initialize the map.
async function initMap() {
await Promise.all([
google.maps.importLibrary("maps"),
google.maps.importLibrary("geocoding"),
google.maps.importLibrary("marker"),
]);
const mapElement = document.querySelector("gmp-map");
const innerMap = mapElement.innerMap;
const geocoder = new google.maps.Geocoder();
const infowindow = new google.maps.InfoWindow();
document.getElementById("submit").addEventListener("click", () => {
geocodePlaceId(geocoder, innerMap, infowindow);
});
}
// This function is called when the user clicks the UI button.
function geocodePlaceId(geocoder, map, infowindow) {
const placeId = document.getElementById("place-id")
.value;
geocoder
.geocode({ placeId: placeId })
.then(({ results }) => {
if (results[0]) {
map.setZoom(11);
map.setCenter(results[0].geometry.location);
const marker = new google.maps.marker.AdvancedMarkerElement({
map,
position: results[0].geometry.location,
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
else {
window.alert("No results found");
}
})
.catch((e) => window.alert("Geocoder failed due to: " + e));
}
initMap();
// [END maps_geocoding_place_id]