-
Notifications
You must be signed in to change notification settings - Fork 3
/
map.js
465 lines (397 loc) · 16.2 KB
/
map.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
var app_config = {
geojson_feeds: {
Cantons: 'geojson/G3K12.geojson'
},
area_mask_fusion_tables_query: null,
styles: {
polygon_draggable: {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.1,
visible: true
},
polygon_final: {
strokeColor: '#347C17',
fillColor: '#347C2C',
fillOpacity: 0.8
}
}
};
$(document).delegate("#map_page", "pageinit", function(){
setTimeout(function(){
$("#game_init").panel("open");
}, 1000);
});
$(document).delegate("#map_page", "pagebeforecreate", function(){
var ua_is_mobile = navigator.userAgent.indexOf('iPhone') !== -1 || navigator.userAgent.indexOf('Android') !== -1;
if (ua_is_mobile) {
$('body').addClass('mobile');
}
var overlays = [];
var app_data = (function(){
var data = [];
$.each(app_config.geojson_feeds, function(k, geojson_url){
var overlays = [];
var area_bounds = new google.maps.LatLngBounds();
$.ajax(geojson_url, {
dataType: "json",
async: false,
success: function(data) {
$.each(data.features, function(k, feature){
var feature_polygons = null;
if ((feature.geometry.type === 'Polygon')) {
feature_polygons = [feature.geometry.coordinates];
} else {
feature_polygons = feature.geometry.coordinates;
}
var paths = [];
var bounds = new google.maps.LatLngBounds();
$.each(feature_polygons, function(k, polygon_coordinates){
$.each(polygon_coordinates, function(k, feature_paths){
var path = [];
$.each(feature_paths, function(k, point){
var latlng = new google.maps.LatLng(point[1], point[0]);
path.push(latlng);
bounds.extend(latlng);
area_bounds.extend(latlng);
});
paths.push(path);
});
});
var overlay = {
paths: paths,
bounds: bounds,
properties: feature.properties,
id: k
};
overlays.push(overlay);
});
}
});
data.push({
name: k,
overlays: overlays,
bounds: area_bounds
});
});
return data;
})();
var game_type = (function(){
var type_id = 0;
if (app_data.length > 1) {
var game_type_rows = [];
$.each(app_data, function(k, row){
var game_type_row = '<input type="radio" name="game_type" id="game_type_' + row.name + '" value="' + k + '"' + (k === 0 ? ' checked="checked"' : '') + ' /><label for="game_type_' + row.name + '">' + row.name + '</label>';
game_type_rows.push(game_type_row);
});
$('#game_types').html(game_type_rows.join("\n"));
$('#game_type').removeClass('hidden');
}
function setTypeId(value) {
type_id = value;
$('#polygon_stats').html("0/" + app_data[type_id].overlays.length);
overlays = app_data[type_id].overlays;
}
setTypeId(type_id);
$('#game_type input:radio').change(function(){
setTypeId($(this).val());
});
return {
getId: function(){
return type_id;
}
};
})();
$("#game_init").panel({
display: 'push',
dismissible: false
});
$("#game_end").panel({
display: 'push',
dismissible: false,
position: 'right'
});
$("#game_info").panel({
display: 'push',
dismissible: false,
position: 'right'
});
var mask_layer = null;
var map = (function(){
var mapBounds = app_data[game_type.getId()].bounds;
var mapOptions = {
center: mapBounds.getCenter(),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
panControl: false,
mapTypeControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
}
};
var map = new google.maps.Map($("#map_canvas")[0], mapOptions);
map.fitBounds(mapBounds);
if (app_config.area_mask_fusion_tables_query !== null) {
mask_layer = new google.maps.FusionTablesLayer({
suppressInfoWindows: true,
clickable: false,
map: map,
query: app_config.area_mask_fusion_tables_query
});
}
var el = $('#social')[0];
map.controls[google.maps.ControlPosition.TOP_CENTER].push(el);
el = $('#game_bar_help')[0];
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(el);
return map;
})();
(function(){
var styles = {
'easy': [
{
"featureType": "road",
"stylers": [
{ "weight": 0.4 }
]
},{
"featureType": "road",
"elementType": "labels",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "road.highway",
"stylers": [
{ "lightness": 40 }
]
},{
"featureType": "transit",
"stylers": [
{ "lightness": -10 },
{ "weight": 1 }
]
}
],
'hard': [
{
"elementType": "labels",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "road",
"elementType": "labels",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "administrative",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "road",
"stylers": [
{ "weight": 0.5 },
{ "lightness": 10 }
]
},{
"featureType": "road.highway",
"stylers": [
{ "lightness": 70 }
]
},{
"featureType": "transit",
"stylers": [
{ "lightness": -10 },
{ "weight": 1 }
]
}
]
};
function mapSetStyles(value) {
map.setOptions({
styles: styles[value]
});
if (mask_layer !== null) {
mask_layer.setMap(value === 'easy' ? map : null);
}
}
var selector = $('#difficulty input:radio');
selector.change(function(){
mapSetStyles($(this).val());
});
mapSetStyles(selector.val());
})();
(function(){
var timer = (function(){
var s_no = 0;
var interval = 0;
function paintTimer() {
function pad(val) {
val = '0' + val;
return val.substr(val.length - 2);
}
var minutes = Math.floor(s_no / 60);
var seconds = s_no - (minutes * 60);
$('#timer_stats').html(pad(minutes) + ':' + pad(seconds));
s_no += 1;
}
function start() {
s_no = 0;
interval = setInterval(paintTimer, 1000);
$('#stats_time').removeClass('hidden');
}
function stop() {
clearInterval(interval);
$('#stats_time').addClass('hidden');
}
return {
start: start,
stop: stop
};
})();
var ids_notpainted, ids_matched_no, lastPolygon;
function map_reset() {
ids_notpainted = [];
$.each(overlays, function(k, overlay){
if ((typeof overlay.polygon) !== 'undefined') {
overlay.polygon.setMap(null);
}
ids_notpainted.push(overlay.id);
});
ids_matched_no = 0;
lastPolygon = null;
$('body').off('keyup');
}
function ui_init() {
if (ua_is_mobile === false) {
$('#game_bar_help').removeClass('vis_hidden');
}
$('#load_polygon').button("enable");
timer.start();
$('#stats_info').removeClass('hidden');
paintPolygon();
$( "#target" ).keyup(function() {
alert( "Handler for .keyup() called." );
});
$('body').on('keyup', function(e){
if (e.keyCode === 32) {
paintPolygon();
}
});
google.maps.event.addListener(map, 'click', paintPolygon);
}
var infowindow = new google.maps.InfoWindow({
maxWidth: 200
});
$("#game_init").on("panelopen", map_reset);
$("#game_init").on("panelclose", ui_init);
function paintPolygon() {
if (ids_notpainted.length === 0) {
$('#load_polygon').button("disable");
return;
}
var random_index = Math.floor((Math.random()*ids_notpainted.length));
var overlay_id = ids_notpainted[random_index];
var overlay = overlays[overlay_id];
ids_notpainted.splice(random_index, 1);
overlay.polygon = new google.maps.Polygon({
paths: overlay.paths,
map: map,
draggable: true,
zIndex: 2,
geodesic: true,
visible: false
});
overlay.polygon.moveTo(map.getCenter());
overlay.polygon.setOptions(app_config.styles.polygon_draggable);
overlay.polygon.moveTo(map.getCenter());
overlay.polygon.setVisible(true);
overlay.polygon.set('did_not_move', true);
overlay.polygon.set('overlay_id', overlay_id);
// The last polygon wasn't moved by the user, he doesn't know where to put it ?
// => remove it from the map for now.
if ((lastPolygon !== null) && (lastPolygon.get('did_not_move'))) {
lastPolygon.setMap(null);
ids_notpainted.push(lastPolygon.get('overlay_id'));
lastPolygon = null;
}
lastPolygon = overlay.polygon;
google.maps.event.addListener(overlay.polygon, 'dragend', function() {
overlay.polygon.set('did_not_move', false);
var new_bounds = new google.maps.LatLngBounds();
$.each(overlay.polygon.getPaths().getArray(), function(k, path){
$.each(path.getArray(), function(k, point){
new_bounds.extend(point);
});
});
var new_bounds_sw_x = new_bounds.getSouthWest().lng() - 0.05;
var new_bounds_sw_y = new_bounds.getSouthWest().lat() - 0.05;
var new_bounds_sw = new google.maps.LatLng(new_bounds_sw_y, new_bounds_sw_x);
var new_bounds_ne_x = new_bounds.getNorthEast().lng() + 0.05;
var new_bounds_ne_y = new_bounds.getNorthEast().lat() + 0.05;
var new_bounds_ne = new google.maps.LatLng(new_bounds_ne_y, new_bounds_ne_x);
new_bounds = new google.maps.LatLngBounds(new_bounds_sw, new_bounds_ne);
if (new_bounds.contains(overlay.bounds.getSouthWest()) && new_bounds.contains(overlay.bounds.getNorthEast())) {
overlay.polygon.setPaths(overlay.paths);
overlay.polygon.setOptions({
draggable: false,
zIndex: 1
});
overlay.polygon.setOptions(app_config.styles.polygon_final);
google.maps.event.addListener(overlay.polygon, 'click', function(ev) {
if (typeof(overlay.properties.name) === 'undefined') {
return;
}
var window_content = '<div><b>' + overlay.properties.name + '</b>';
if (typeof(overlay.properties.icon) !== 'undefined') {
window_content += '<br/><img class="geometry" src="' + overlay.properties.icon + '"/>';
}
if (typeof(overlay.properties.url) !== 'undefined') {
window_content += '<br/><a href="' + overlay.properties.url + '" target="_blank">More info</a>';
}
window_content += '</div>';
infowindow.setPosition(ev.latLng);
infowindow.setContent(window_content);
infowindow.open(map);
});
ids_matched_no += 1;
$('#polygon_stats').html(ids_matched_no + "/" + overlays.length);
paintPolygon();
if ((typeof (overlay.properties.name)) !== 'undefined') {
$('#game_bar_help').html(overlay.properties.name);
google.maps.event.trigger(map, 'resize');
$('#game_bar_help').removeClass('vis_hidden');
setTimeout(function(){
$('#game_bar_help').addClass('vis_hidden');
}, 5000);
}
if (ids_matched_no === overlays.length) {
timer.stop();
var timer_stats = $('#timer_stats').html();
$('#timer_stats_final').html(timer_stats);
var tweet_text = 'Solved the Swiss GeoPuzzle in ' + timer_stats + ' ! Who can beat my time ?';
$('#game_end_tweet_placeholder').html('<iframe allowtransparency="true" frameborder="0" scrolling="no" src="https://platform.twitter.com/widgets/tweet_button.html?hashtags=quiz&count=none&text=' + encodeURIComponent(tweet_text) + '" style="width:100px; height:20px;"></iframe>');
$("#game_end").panel("open");
}
}
});
}
$('#load_polygon').click(paintPolygon);
$('.new_game').click(function(){
var yn = null;
if ($(this).attr('data-ignore-warnings') === 'yes') {
yn = true;
} else {
yn = confirm('Are you sure ? The current game progress will be lost !');
}
if (yn) {
timer.stop();
$("#game_init").panel("open");
}
});
})();
});