-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdemo.js
More file actions
85 lines (81 loc) · 3.46 KB
/
demo.js
File metadata and controls
85 lines (81 loc) · 3.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"use strict";
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
const placeAutocompleteElement = document.querySelector('gmp-basic-place-autocomplete');
const placeDetailsElement = document.querySelector('gmp-place-details-compact');
const placeDetailsParent = placeDetailsElement.parentElement;
const gmpMapElement = document.querySelector('gmp-map');
async function initMap() {
// Asynchronously load required libraries from the Google Maps JS API.
await google.maps.importLibrary('places');
const { AdvancedMarkerElement } = (await google.maps.importLibrary('marker'));
const { InfoWindow } = (await google.maps.importLibrary('maps'));
// Get the initial center directly from the gmp-map element's property.
const center = gmpMapElement.center;
// Set the initial location bias for the autocomplete element.
placeAutocompleteElement.locationBias = center;
// Update the map object with specified options.
const map = gmpMapElement.innerMap;
map.setOptions({
clickableIcons: false,
mapTypeControl: false,
streetViewControl: false,
});
// Create an advanced marker to show the location of a selected place.
const advancedMarkerElement = new AdvancedMarkerElement({
map: map,
collisionBehavior: google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL,
});
// Create an InfoWindow to hold the place details component.
const infoWindow = new InfoWindow({
minWidth: 360,
disableAutoPan: true,
headerDisabled: true,
pixelOffset: new google.maps.Size(0, -10),
});
// Event listener for when a place is selected from the autocomplete list.
placeAutocompleteElement.addEventListener('gmp-select', (event) => {
// Reset marker and InfoWindow, and prepare the details element.
placeDetailsParent.appendChild(placeDetailsElement);
placeDetailsElement.style.display = 'block';
advancedMarkerElement.position = null;
infoWindow.close();
// Request details for the selected place.
const placeDetailsRequest = placeDetailsElement.querySelector('gmp-place-details-place-request');
placeDetailsRequest.place = event.place.id;
});
// Event listener for when the place details have finished loading.
placeDetailsElement.addEventListener('gmp-load', () => {
const location = placeDetailsElement.place
.location;
// Position the marker and open the InfoWindow at the place's location.
advancedMarkerElement.position = location;
infoWindow.setContent(placeDetailsElement);
infoWindow.open({
map,
anchor: advancedMarkerElement,
});
map.setCenter(location);
});
// Event listener to close the InfoWindow when the map is clicked.
map.addListener('click', () => {
infoWindow.close();
advancedMarkerElement.position = null;
});
// Event listener for when the map finishes moving (panning or zooming).
map.addListener('idle', () => {
const newCenter = map.getCenter();
// Update the autocomplete's location bias to a 10km radius around the new map center.
placeAutocompleteElement.locationBias = new google.maps.Circle({
center: {
lat: newCenter.lat(),
lng: newCenter.lng(),
},
radius: 10000, // 10km in meters.
});
});
}
initMap();