-
Notifications
You must be signed in to change notification settings - Fork 821
/
index.js
executable file
·53 lines (52 loc) · 2.08 KB
/
index.js
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
// [START maps_places_placeid_geocoder]
// This sample requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script
// src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
});
const input = document.getElementById("pac-input");
const autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo("bounds", map);
// Specify just the place data fields that you need.
autocomplete.setFields(["place_id", "geometry", "name", "formatted_address"]);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
const infowindow = new google.maps.InfoWindow();
const infowindowContent = document.getElementById("infowindow-content");
infowindow.setContent(infowindowContent);
const geocoder = new google.maps.Geocoder();
const marker = new google.maps.Marker({ map: map });
marker.addListener("click", () => {
infowindow.open(map, marker);
});
autocomplete.addListener("place_changed", () => {
infowindow.close();
const place = autocomplete.getPlace();
if (!place.place_id) {
return;
}
geocoder.geocode({ placeId: place.place_id }, (results, status) => {
if (status !== "OK" && results) {
window.alert("Geocoder failed due to: " + status);
return;
}
map.setZoom(11);
map.setCenter(results[0].geometry.location);
// Set the position of the marker using the place ID and location.
marker.setPlace({
placeId: place.place_id,
location: results[0].geometry.location,
});
marker.setVisible(true);
infowindowContent.children["place-name"].textContent = place.name;
infowindowContent.children["place-id"].textContent = place.place_id;
infowindowContent.children["place-address"].textContent =
results[0].formatted_address;
infowindow.open(map, marker);
});
});
}
// [END maps_places_placeid_geocoder]