Use dedicated function to prepare PlPage for maps.
[platal.git] / htdocs / javascript / maps.js
CommitLineData
45dcbe4d
SJ
1/***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21// http://code.google.com/apis/maps/documentation/javascript/
dcc9c952 22// http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries
45dcbe4d 23
9b5a02f7 24function map_initialize()
45dcbe4d 25{
9b5a02f7 26 var latlng = new google.maps.LatLng(0, 0);
45dcbe4d
SJ
27 var myOptions = {
28 zoom: 1,
29 center: latlng,
30 mapTypeId: google.maps.MapTypeId.ROADMAP
31 };
32 var map = new google.maps.Map($('#map_canvas').get(0), myOptions);
dbbae4f7
SJ
33
34 $.xget('map/ajax', function(json_data) {
35 var data = jQuery.parseJSON(json_data);
36 var dots = data.data;
37 var count = dots.length;
38 var markers = [];
39
40 for (var i = 0; i < count; ++i) {
41 var latLng = new google.maps.LatLng(dots[i].latitude, dots[i].longitude);
ae1938f6 42 var color = promos_to_color(dots[i].promo);
dcc9c952
SJ
43
44 if (dots[i].hrpid.search(',') > -1) {
45 var hrpids = dots[i].hrpid.split(',');
46 var names = dots[i].name.split(',');
47 var link_array = new Array();
48
49 for (var j = 0; j < hrpids.length; ++j) {
50 link_array[j] = '<a href="profile/' + hrpids[j] + '">' + names[j] + '</a>';
51 }
52 var link = link_array.join('<br />');
53 } else {
54 var link = '<a href="profile/' + dots[i].hrpid + '">' + dots[i].name + '</a>';
55 }
56
57 var marker = new MarkerWithLabel({
58 'position': latLng,
59 'labelContent': link,
60 'labelClass': 'marker_label'
61 });
ae1938f6
SJ
62 marker.bindTo('icon', new ColoredIcon(color));
63 marker.set('color', color);
dbbae4f7
SJ
64 markers.push(marker);
65 }
66 var mc = new MarkerClusterer(map, markers);
67 });
45dcbe4d
SJ
68}
69
ae1938f6
SJ
70function ColoredIcon(color)
71{
72 this.set('starcolor', null);
73 this.set('color', color);
74 this.set('icon', 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=|' + color);
75}
76
77ColoredIcon.prototype = new google.maps.MVCObject();
78
79var colors = new Array();
80colors['red'] = 'ff0000';
81colors['yellow'] = 'ffff00';
82colors['blue'] = '0000ff';
83colors['green'] = '00ff00';
84colors['gray'] = '606060';
85
86function promos_to_color(promos)
87{
88 var promos_array = promos.split(',');
89 var length = promos_array.length;
90
91 if (length == 1) {
92 return colors[promo_to_color(promos)];
93 }
94
95 var color_array = new Array();
96 for (var i = 0; i < length; ++i) {
97 color_array[i] = promo_to_color(promos_array[i]);
98 }
99
100 return color_average(color_array);
101}
102
103function promo_to_color(promo)
104{
105 var main_education = promo.charAt(0);
106
107 switch (main_education)
108 {
109 case 'X':
110 var year_promo = promo.substr(1);
111 if ((year_promo % 2) == 0) {
112 return 'red';
113 } else {
114 return 'yellow';
115 }
116 case 'M':
117 return 'green';
118 case 'D':
119 return 'blue';
120 default:
121 return 'gray';
122 }
123}
124
125function color_average(color_array)
126{
127 var length = color_array.length;
128 var rbg = new Array(0, 0, 0);
129
130 for (var i = 0; i < length; ++i) {
131 switch (color_array[i])
132 {
133 case 'red':
134 rbg[0] += 1;
135 break;
136 case 'yellow':
137 rbg[0] += 1;
138 rbg[1] += 1;
139 break;
140 case 'blue':
141 rbg[1] += 1;
142 break;
143 case 'green':
144 rbg[2] += 1;
145 break;
146 case 'gray':
147 rbg[0] += 0.5;
148 rbg[1] += 0.5;
149 rbg[2] += 0.5;
150 break;
151 default:
152 break;
153 }
154 }
155
156 var color_code = '';
157 for (var i = 0; i < 3; ++i) {
158 var color = Math.floor(rbg[i] / length * 255);
159 var hexa = color.toString(16);
160 if (hexa.length == 1) {
161 hexa += '0';
162 }
163 color_code += hexa;
164 }
165
166 return color_code;
167}
168
45dcbe4d 169// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: