-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
676 lines (544 loc) · 18.9 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
if (!String.fmt) {
String.prototype.fmt = function() {
var formatted = this;
var i;
for (i=0; i<arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
}
function haversine(lat1, lng1, lat2, lng2) {
lat1 = lat1 / 180 * Math.PI;
lat2 = lat2 / 180 * Math.PI;
lng1 = lng1 / 180 * Math.PI;
lng2 = lng2 / 180 * Math.PI;
return 2 * 6371.0 * Math.asin(Math.sqrt(
Math.pow(Math.sin(0.5 * (lat1 - lat2)), 2) +
Math.pow(Math.sin(0.5 * (lng1 - lng2)), 2) *
Math.cos(lat1) * Math.cos(lat2)));
}
/**
*
* Popup management
*
*/
/**
* Loading popup
* @param {string} divId Container for the popup
* @see #centerPopup
* @see #disablePopup
*/
function loadPopup(divId) {
// First background fade in
$("#backgroundPopup").css({
"opacity": "0.7"
});
$("#backgroundPopup").fadeIn("slow");
// Then the popup over the background
$(divId).fadeIn("slow");
}
/**
* Disabling popup
* @param {string} divId Container for the popup
* @see #loadPopup
*/
function disablePopup(divId) {
// Background fade out
$("#backgroundPopup").fadeOut("slow");
// Popup fade out
$(divId).fadeOut("slow");
}
/**
* Centering popup
* @param {string} divId Container for the popup
* @see #loadPopup
*/
function centerPopup(divId) {
// Using jQuery functions to
// have cross-browser compatibility
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var popupHeight = $(divId).height();
var popupWidth = $(divId).width();
// Centering
$(divId).css({
//"position": "absolute",
"top": windowHeight/2 - popupHeight/2,
"left": windowWidth/2 - popupWidth/2
});
}
function sortPointX(a, b) {
return a.lng() - b.lng();
}
function sortPointY(a, b) {
return a.lat() - b.lat();
}
function overflow(text) {
if (typeof text !== "string") {
return text;
}
if (text.length < 30) {
return text;
}
return ('' + text).slice(0, 25) + '...';
}
function initialize(jsonData) {
// Set map options
var parisLocation = new google.maps.LatLng(48.8, 2.33);
var mapTypeIds = [];
// Creating list of available map types, adding OSM
for(var mtype in google.maps.MapTypeId) {
if (google.maps.MapTypeId.hasOwnProperty(mtype)) {
mapTypeIds.push(google.maps.MapTypeId[mtype]);
}
}
mapTypeIds.push("MQ");
mapTypeIds.push("OSM");
var mapOptions = {
zoom : 2,
center : parisLocation,
mapTypeId : "OSM",
mapTypeControlOptions: {
mapTypeIds: mapTypeIds
}
};
// Create the map
var map = new google.maps.Map(document.getElementById("canvas"), mapOptions);
// Setting tiles
map.mapTypes.set("MQ", new google.maps.ImageMapType({
getTileUrl : function(coord, zoom) {
return "http://otile3.mqcdn.com/tiles/1.0.0/osm/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
},
tileSize : new google.maps.Size(256, 256),
name : "MapQuest",
maxZoom : 18
}));
map.mapTypes.set("OSM", new google.maps.ImageMapType({
getTileUrl : function(coord, zoom) {
return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
},
tileSize : new google.maps.Size(256, 256),
name : "OpenStreetMap",
maxZoom : 18
}));
if (jsonData.points.length === 0){
return;
}
// For fields order
var fields = [];
var field;
var n = jsonData.points.length;
var icon_color = jsonData.meta.icon_color;
var icon_weight = jsonData.meta.icon_weight;
var icon_type = jsonData.meta.icon_type;
var base_icon = jsonData.meta.base_icon;
//var link_duplicates = jsonData.meta.link_duplicates;
var toggle_lines = jsonData.meta.toggle_lines;
var with_icons = icon_type !== null;
var with_circles = icon_weight !== null;
var with_colors = icon_color !== null;
var getMarkerIcon = function (color) { return color + '_' + base_icon; };
var getCircleColor;
if (with_icons || ! with_colors) {
getCircleColor = function (color) { return 'black'; };
} else {
getCircleColor = function (color) { return color; };
}
var markersArray = [];
var circlesArray = [];
var centersArray = [];
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
// closure fun
function closeInfoWindow() {
var i, c;
for (i=0, c=markersArray.length; i<c; i++) {
infowindow.close(map, markersArray[i]);
}
}
var i, j, c, e, w, latlng, marker, circle, circle_col, has_circle;
var max_value = 0;
// Load the data
for (i=0 ; i<n ; i++) {
e = jsonData.points[i];
w = parseFloat(e.__wei__);
latlng = new google.maps.LatLng(e.lat, e.lng);
if (isNaN(latlng.lat()) || isNaN(latlng.lng())) {
//console.log(e.__lab__ + ' had no position: ' + e.lat + e.lng)
continue;
}
has_circle = (! isNaN(w)) && w > 0;
circle_col = getCircleColor(e.__col__);
if (! with_icons && ! has_circle) {
// If no markers, only circles are displayed.
// If no circle too, let's move on
// Note that lines will not go through these
continue;
}
marker = new google.maps.Marker({
position : latlng,
animation : null,
title : e.__lab__,
hidden : e.__hid__, // if fetched from join clause
clickable : true,
draggable : false
});
if (with_icons) {
if (! marker.hidden) {
marker.setMap(map);
}
marker.setIcon(getMarkerIcon(e.__col__));
}
// Augmenting the marker type
marker.help = ' ' +
'<div class="infowindow medium" style="min-width: 400px;">' +
'<h3>{0}</h3>'.fmt(e.__lab__) +
'<table cellpadding="1">';
// Computing fields order
fields.length = 0;
for (field in e) {
if (e.hasOwnProperty(field)) {
if (field !== '__lab__' && field !== '__col__') {
fields.push(field);
}
}
}
fields.sort();
for (j=0 ; j<fields.length ; j++) {
marker.help += '<tr><td><i>{0}</i></td><td>{1}</td></tr>'.fmt(fields[j], overflow(e[fields[j]]));
}
marker.help += ' ' +
'</table>' +
'</div>';
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.help);
infowindow.open(map, this);
});
google.maps.event.addListener(marker, 'rightclick', function () {
if (this.getAnimation() === null) {
this.setAnimation(google.maps.Animation.BOUNCE);
} else {
this.setAnimation(null);
}
});
// Saving marker
markersArray.push(marker);
bounds.extend(latlng);
if (! marker.hidden) {
centersArray.push(latlng);
}
if (has_circle) {
// We compute the biggest __wei__ value
if (w > max_value) {
max_value = w;
}
circle = new google.maps.Circle({
center : latlng,
radius : 0,
strokeColor : circle_col,
strokeOpacity : 0.25,
strokeWeight : 2,
fillColor : circle_col,
fillOpacity : 0.15,
map : map,
clickable : true
});
// Augmenting the marker type
circle.weight = w;
circle.help = ' ' +
'<div class="infowindow medium">' +
'<h3>{0}</h3>'.fmt(e.__lab__) +
'<i>{0}</i> {1}<br/>'.fmt(icon_weight, w);
if (with_colors) {
circle.help += '<i>{0}</i> {1} ({2})'.fmt(icon_color, e.__cat__, e.__col__);
}
circle.help += ' ' +
'</div>';
// Saving
circlesArray.push(circle);
google.maps.event.addListener(circle, 'click', function(event) {
infowindow.setContent(this.help);
infowindow.open(map, new google.maps.Marker({position : event.latLng}));
});
}
}
// Ratio for circles surface on the map
//var R = 100;
var r = 0.15;
function updateLabel(value) {
// We update the span where r is displayed
$('#ratio').html(parseInt(100 * parseFloat(value), 10) + '%');
}
function updateCircles(value) {
// We compute the top radius given the map surface
var mapBounds = map.getBounds();
var sw = mapBounds.getSouthWest();
var ne = mapBounds.getNorthEast();
var biggest = 0.5 * r * 1000 * haversine(sw.lat(), sw.lng(), ne.lat(), ne.lng());
//var biggest = R * 1000;
for (i=0, c=circlesArray.length; i<c; i++) {
circle = circlesArray[i];
circle.setRadius(Math.sqrt(circle.weight / max_value) * biggest);
}
}
$( "#slider" ).slider({
range : false,
min : 0,
max : 1.00,
step : 0.01,
value : r,
//animate : 'slow',
slide : function (event, ui) {
updateLabel(ui.value);
},
stop : function (event, ui) {
// Updating global variable
r = parseFloat(ui.value);
updateCircles();
}
});
// We trigger manually an updateCircles once the fitBounds is finished
updateLabel(r);
google.maps.event.addListenerOnce(map, 'bounds_changed', updateCircles);
google.maps.event.addListener(map, 'zoom_changed', updateCircles);
// If no markers, we avoid a big
// drift to the pacific ocean :)
if (n >= 2 && (with_icons || with_circles)) {
map.fitBounds(bounds);
}
// Add specified lines
var ods, type, lcol, coords, line, d, help;
var linesArray = [];
var lineTypes = {};
for (i=0, c=jsonData.lines.length; i<c; i++) {
ods = jsonData.lines[i].path;
type = jsonData.lines[i].__lab__;
lcol = jsonData.lines[i].__col__;
if (! lineTypes.hasOwnProperty(type)){
lineTypes[type] = 0;
}
lineTypes[type] += 1;
coords = [];
help = '<div class="infowindow medium">' +
'<h3>{0}</h3><table>'.fmt(type) +
'<tr><td><i>{0}</i></td><td><i>{1}</i></td></tr>'.fmt('Key', 'Label');
for (j=0, d=ods.length; j<d; j++) {
latlng = new google.maps.LatLng(ods[j].lat, ods[j].lng);
if (! isNaN(latlng.lat()) && ! isNaN(latlng.lng())) {
coords.push(latlng);
help += '<tr><td>{0}</td><td>{1}</td></tr>'.fmt(ods[j].__key__, ods[j].__lab__);
}
}
help += '</table></div>';
line = new google.maps.Polyline({
map : null, // not drawn by default
geodesic : true,
clickable : true,
path : coords,
type : type,
strokeColor : lcol,
strokeOpacity : 0.4,
strokeWeight : 5
});
line.help = help;
google.maps.event.addListener(line, 'click', function(event) {
infowindow.setContent(this.help);
infowindow.open(map, new google.maps.Marker({position : event.latLng}));
});
linesArray.push(line);
}
// Lines handling
var lineTypesArray = [];
for (type in lineTypes){
if (lineTypes.hasOwnProperty(type)) {
lineTypesArray.push([type, lineTypes[type]]);
}
}
// Pushing null type to have a mode where lines are not displayed
lineTypesArray.push([null, 0]);
// Sorting with decreasing volumes
lineTypesArray.sort(function (a, b) {
return b[1] - a[1];
});
var toggled = false;
var lines_state = 0;
var drawn_type, prev_drawn_type = null;
function drawLines() {
if (lineTypesArray.length === 0){
return;
}
prev_drawn_type = drawn_type;
drawn_type = lineTypesArray[lines_state][0];
$('#lines').text('Lines ({0})'.fmt(drawn_type === null ? 'none' : drawn_type.toLowerCase()));
var i, c;
for (i=0, c=linesArray.length; i<c; i++) {
if (linesArray[i].type === drawn_type) {
linesArray[i].setMap(map);
} else {
linesArray[i].setMap(null);
}
}
// Special appearance of hidden markers when displaying join lines
if (drawn_type === 'Joined') {
for (i=0, c=markersArray.length; i<c; i++) {
if (markersArray[i].hidden) {
markersArray[i].setMap(map);
}
}
} else if (prev_drawn_type === 'Joined') {
for (i=0, c=markersArray.length; i<c; i++) {
if (markersArray[i].hidden) {
markersArray[i].setMap(null);
}
}
}
lines_state += 1;
lines_state = lines_state === lineTypesArray.length ? 0 : lines_state;
}
$('#lines').click(drawLines);
if (toggle_lines) {
// If the user defined some lines in input
// We do not wait for him to click on the button
drawLines();
}
if (jsonData.lines.length === 0) {
// If not duplicates, we disable the button
$('#lines').text('Lines');
$('#lines').attr('disabled', 'true');
$('#lines').css({'color': 'grey'});
}
// Draw hull
var hull = new google.maps.Polyline({
path : centersArray,
strokeColor : "green",
strokeOpacity : 0.40,
strokeWeight : 3,
//fillColor : "green",
//fillOpacity : 0,
geodesic : true,
clickable : false
});
// Control lines state when clicking on button
var state = 0;
var sortedCenters;
function linkMarkers() {
if (state === 0) {
hull.setPath(centersArray);
hull.setMap(map);
$('#link').text('Link all (sort: 0)');
} else if (state === 1) {
sortedCenters = centersArray.slice();
sortedCenters.sort(sortPointY);
sortedCenters.sort(sortPointX);
hull.setPath(sortedCenters);
$('#link').text('Link all (sort: X)');
} else if (state === 2) {
sortedCenters = centersArray.slice();
sortedCenters.sort(sortPointX);
sortedCenters.sort(sortPointY);
hull.setPath(sortedCenters);
$('#link').text('Link all (sort: Y)');
} else if (state === 3) {
hull.setMap(null);
$('#link').text('Link all (none)');
}
state += 1;
state = state === 4 ? 0 : state;
}
$('#link').click(linkMarkers);
google.maps.event.addListener(map, 'rightclick', function () {
linkMarkers();
// If the rightclick shall go through
//$(this).trigger();
});
// Fill legend
var cat, vol, col, row, icon;
var msg = ' ' +
'<table id="legendcontent" class="medium">' +
'<tr><th><i>Icon</i></th><th><i>Color</i></th><th><i>Circle</i></th><th><i>Category</i></th><th><i>Volume</i></th></th>';
if (with_icons) {
row = '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3} "{4}"</td><td>{5} points</td></tr>';
} else {
row = '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3} "{4}"</td><td>' + icon_weight + ' {5}</td></tr>';
}
for (i=0, c=jsonData.categories.length; i<c ;i++) {
cat = jsonData.categories[i][0];
col = jsonData.categories[i][1].color;
vol = jsonData.categories[i][1].volume;
if (with_icons) {
icon = '<img src="{0}" alt="icon"/>'.fmt(getMarkerIcon(col));
} else {
icon = '(none)';
}
msg += row.fmt(icon, col, getCircleColor(col), icon_color, cat, vol);
}
msg += '</table>';
// General information
$('#legendPopup').html(msg);
$('#info').html('{0} <i>point(s) on map</i> (out of {1}), {2} <i>line(s)</i>, {3} <i>{4}</i> categorie(s), <i>{5}</i> max: {6}'.fmt(markersArray.length, n, jsonData.lines.length, jsonData.categories.length, icon_color, icon_weight, max_value));
// Press Escape event!
// Use keydown instead of keypress for webkit-based browsers
$(document).keydown(function (e) {
if (e.keyCode === 27) {
disablePopup('#legendPopup');
closeInfoWindow();
}
});
}
$(document).ready(function() {
$('#backgroundPopup').css({
'display' : 'none',
'position' : 'fixed',
'_position' : 'absolute', /* hack for internet explorer 6*/
'height' : '100%',
'width' : '100%',
'top' : '0',
'left' : '0',
'background' : '#000000',
'border' : '0px solid #cecece',
'z-index' : '1'
});
$('#legendPopup').css({
'display' : 'none',
'position' : 'fixed',
'_position' : 'absolute', /* hack for internet explorer 6*/
'height' : '500px',
'width' : '750px',
'padding' : '0px',
'text-align' : 'center',
'color' : 'white',
'background' : 'rgba(0, 0, 0, 0)',
'border' : '0px solid #cecece',
'z-index' : '2'
});
$('#legend').click(function() {
centerPopup('#legendPopup');
loadPopup('#legendPopup');
});
$("#backgroundPopup").click(function () {
disablePopup('#legendPopup');
});
$('#legend').attr('title', 'Display legend.');
$('#link').attr('title', 'Draw lines between points. Click again to change sorting.');
$('#lines').attr('title', 'Display lines. Click again to change line type.');
$('#ratio').attr('title', 'Circle size (%)');
$('#slider').attr('title', 'Circle size (%)');
// This is weird, but $(window).height seems to change after
// document is ready
setTimeout(function () {
$("#canvas").css({
"height": $(window).height() * 0.90
});
$("#canvas").css({
"width": $(window).width() * 0.99
});
}, 300);
setTimeout(function () {
// JSON_FILE is defined in the template
$.getJSON(JSON_FILE, function(data){
initialize(data);
});
}, 500);
});