-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaces.html
More file actions
45 lines (40 loc) · 1.4 KB
/
places.html
File metadata and controls
45 lines (40 loc) · 1.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Google Places Autocomplete Example</title>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
function initAutocomplete() {
const input = document.createElement("input");
document.body.appendChild(input);
const autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.addListener("place_changed", () => {
const place = autocomplete.getPlace();
console.log(place);
});
const autocompleteService =
new google.maps.places.AutocompleteService();
input.addEventListener("input", () => {
const query = input.value;
if (query.length < 3) {
return;
}
autocompleteService.getPlacePredictions(
{ input: query },
(predictions, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log("Predictions:", predictions);
}
}
);
});
}
initAutocomplete();
});
</script>
</head>
<body></body>
</html>