-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
275 lines (241 loc) · 7.28 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var cities = {
"Europe": {
// switz
"Zurich": { lat: 47.376887, lng: 8.541694 },
"Geneva": { lat: 46.204391, lng: 6.143158 },
"Luzern": { lat: 47.050168, lng: 8.309307 },
// italy
"Milan": { lat: 45.464204, lng: 9.189982 },
"Rome": { lat: 41.902783, lng: 12.496366 },
"Genoa": { lat: 44.40565, lng: 8.946256 },
"Torino": { lat: 45.070312, lng: 7.686856 },
"Venice": { lat: 45.440847, lng: 12.315515 },
// england
"London": { lat: 51.507351, lng: -0.127758 },
// ireland
"Dublin": { lat: 53.349805, lng: -6.26031 },
// germany
"Berlin": { lat: 52.520007, lng: 13.404954 },
"Cologne": { lat: 50.937531, lng: 6.960279 },
// france
"Paris": { lat: 48.856614, lng: 2.352222 },
"Nice": { lat: 43.710173, lng: 7.261953 }
},
"Asia": {
// japan
"Tokyo": { lat: 35.689487, lng: 139.691706 },
// china
"Hong Kong": { lat: 22.396428, lng: 114.109497 },
"Huangshan": { lat: 29.714699, lng: 118.337521 },
"Chengdu": { lat: 30.572815, lng: 104.066801 },
"Dujiangyan": { lat: 31.005165, lng: 103.607531 },
"Beijing": { lat: 39.9042, lng: 116.407396 },
"Shanghai": { lat: 31.23039, lng: 121.473702 },
"Hangzhou": { lat: 30.274084, lng: 120.15507 },
"Qingdao": { lat: 36.067108, lng: 120.382609 },
"Shantou": { lat: 23.354091, lng: 116.681972 }
},
"Australia": {
"Cairns": { lat: -16.918551, lng: 145.778055 },
"Sydney": { lat: -33.86882, lng: 151.209296 },
"Melbourne": { lat: -37.813628, lng: 144.963058 }
},
"North America": {
// usa
"Minneapolis": { lat: 44.977753, lng: -93.265011 },
"St. Paul": { lat: 44.953703, lng: -93.089958 },
"Las Vegas": { lat: 36.169941, lng: -115.13983 },
"Los Angeles": { lat: 34.052234, lng: -118.243685 },
"San Diego": { lat: 32.715738, lng: -117.161084 },
"Buffalo": { lat: 42.886447, lng: -78.878369 },
"New York": { lat: 40.712775, lng: -74.005973 },
"Boston": { lat: 42.360082, lng: -71.05888 },
"Cambridge": { lat: 42.373616, lng: -71.109734 },
"Seatle": { lat: 47.606209, lng: -122.332071 },
"Olympia": { lat: 47.037874, lng: -122.900695 },
"Redding": { lat: 40.58654, lng: -122.391675 },
// canada
"Vancouver": { lat: 49.282729, lng: -123.120738 },
"Kingston": { lat: 44.231172, lng: -76.485954 },
"Calgary": { lat: 51.048615, lng: -114.070846 },
"Tornoto": { lat: 43.653226, lng: -79.383184 }
}
};
// markers currently added
var markers = {
"North America": [],
"Europe": [],
"Asia": [],
"Australia": [],
"Africa": [],
"South America": []
};
function initMap() {
// map constructor
var map = new google.maps.Map(document.getElementById("map"), {
gestureHandling: "auto",
zoom: 4,
center: { lat: 37.871593, lng: -122.272747 }, // berkeley
minZoom: 3,
maxZoom: 20,
mapTypeId: google.maps.MapTypeId.HYBRID
});
function zoomInMarker(marker) {
var marker = marker;
function nested() {
console.log(marker.label.text);
map.setZoom(map.zoom + 6);
map.setCenter(marker.getPosition());
}
return nested;
}
// timeout so not everything drops at once
function addMarkerWithTimeout(country, city, time) {
window.setTimeout(function() {
addMarker(country, city);
}, time);
}
function addMarker(country, city) {
var marker = new google.maps.Marker({
label: {
text: city,
color: "white",
fontSize: "10px"
},
animation: google.maps.Animation.DROP,
position: cities[country][city],
map: map
});
markers[country].push(marker);
google.maps.event.addListener(marker, "click", zoomInMarker(marker));
}
// add berkeley as base
var berkeley = { lat: 37.871593, lng: -122.272747 };
var temp = new google.maps.Marker({
label: {
text: "Berkeley",
color: "white",
fontSize: "10px"
},
animation: google.maps.Animation.DROP,
position: berkeley,
map: map
});
function addCountry(country) {
for (var i = 0; i < markers[country].length; i++) {
markers[country][i].setMap(null);
}
var counter = 0;
for (var city in cities[country]) {
addMarkerWithTimeout(country, city, counter * 50);
counter++;
}
}
/*
FLOW:
1. addCountry
2. addMarkerWithTimeout
3. addMarker
marker gets added in addmarker with all details
*/
// BUTTONS
// init buttons
for (var country in markers) {
// first letter lowercase + 1 to the end for button ID
var id =
country[0].toLowerCase() +
country.slice(1, country.length).replace(/ /g, "") +
"Button";
// nested function
document.getElementById(id).onclick = (function(country) {
var country = country;
function nested() {
// change focus to center when clicked
if (country == "Asia") {
map.setCenter({ lat: 29.714699, lng: 118.337521 }); // Huangshan
} else if (country == "North America") {
map.setCenter({ lat: 40.820744, lng: -96.70047 }); // Nebraska (lol)
} else if (country == "Europe") {
map.setCenter({ lat: 47.050168, lng: 8.309307 });
} else if (country == "Australia") {
map.setCenter({ lat: -28.953512, lng: 135.857048 });
}
function unvisited(country) {
swal({
text: "Haven't visited " + country + " yet!",
width: 300
});
}
// check if visited country yet
if (country === "Africa" || country === "South America") {
return unvisited(country);
} else {
map.setZoom(5);
addCountry(country);
}
}
return nested;
})(country);
}
// all
var all = function() {
for (var country in cities) {
addCountry(country);
}
map.setZoom(3);
};
document.getElementById("all").onclick = all;
// People kept asking me if I've been in Berkeley my entire life
window.onload = all;
// clear
document.getElementById("clear").onclick = function() {
for (var country in cities) {
for (var i = 0; i < markers[country].length; i++) {
markers[country][i].setMap(null);
}
}
};
// zoom out
document.getElementById("out").onclick = function() {
map.setZoom(3);
};
// home
document.getElementById("home").onclick = function() {
map.setCenter(berkeley);
};
// BUTTONS END
// making sure doens't go out of bounds
var lastValidCenter;
setOutOfBoundsListener();
function setOutOfBoundsListener() {
google.maps.event.addListener(map, "dragend", function() {
checkLatitude(map);
});
google.maps.event.addListener(map, "idle", function() {
checkLatitude(map);
});
google.maps.event.addListener(map, "zoom_changed", function() {
checkLatitude(map);
});
}
function checkLatitude(map) {
var bounds = map.getBounds();
var sLat = map
.getBounds()
.getSouthWest()
.lat();
var nLat = map
.getBounds()
.getNorthEast()
.lat();
if (sLat < -85 || nLat > 85) {
//the map has gone beyone the world's max or min latitude - gray areas are visible
//return to a valid position
if (this.lastValidCenter) {
map.setCenter(this.lastValidCenter);
}
} else {
this.lastValidCenter = map.getCenter();
}
}
}