/
index.js
executable file
·45 lines (43 loc) · 1.46 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
// [START maps_event_closure]
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: { lat: -25.363882, lng: 131.044922 },
});
const bounds = {
north: -25.363882,
south: -31.203405,
east: 131.044922,
west: 125.244141,
};
// Display the area between the location southWest and northEast.
map.fitBounds(bounds);
// Add 5 markers to map at random locations.
// For each of these markers, give them a title with their index, and when
// they are clicked they should open an infowindow with text from a secret
// message.
const secretMessages = ["This", "is", "the", "secret", "message"];
const lngSpan = bounds.east - bounds.west;
const latSpan = bounds.north - bounds.south;
for (let i = 0; i < secretMessages.length; ++i) {
const marker = new google.maps.Marker({
position: {
lat: bounds.south + latSpan * Math.random(),
lng: bounds.west + lngSpan * Math.random(),
},
map: map,
});
attachSecretMessage(marker, secretMessages[i]);
}
}
// Attaches an info window to a marker with the provided message. When the
// marker is clicked, the info window will open with the secret message.
function attachSecretMessage(marker, secretMessage) {
const infowindow = new google.maps.InfoWindow({
content: secretMessage,
});
marker.addListener("click", () => {
infowindow.open(marker.get("map"), marker);
});
}
// [END maps_event_closure]