Commit | Line | Data |
---|---|---|
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 | 24 | function map_initialize() |
45dcbe4d | 25 | { |
45dcbe4d SJ |
26 | var myOptions = { |
27 | zoom: 1, | |
e9cd8d9c | 28 | center: new google.maps.LatLng(0, 0), |
45dcbe4d SJ |
29 | mapTypeId: google.maps.MapTypeId.ROADMAP |
30 | }; | |
31 | var map = new google.maps.Map($('#map_canvas').get(0), myOptions); | |
dbbae4f7 | 32 | |
0a928a2f | 33 | $.xget(window.location.href, {ajax: true}, function(json_data) { |
dbbae4f7 SJ |
34 | var data = jQuery.parseJSON(json_data); |
35 | var dots = data.data; | |
36 | var count = dots.length; | |
e9cd8d9c SJ |
37 | var info_window = new google.maps.InfoWindow({}); |
38 | var parameters = ", '_blank', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=840,height=600'"; | |
dbbae4f7 SJ |
39 | var markers = []; |
40 | ||
41 | for (var i = 0; i < count; ++i) { | |
42 | var latLng = new google.maps.LatLng(dots[i].latitude, dots[i].longitude); | |
ae1938f6 | 43 | var color = promos_to_color(dots[i].promo); |
dcc9c952 SJ |
44 | |
45 | if (dots[i].hrpid.search(',') > -1) { | |
46 | var hrpids = dots[i].hrpid.split(','); | |
47 | var names = dots[i].name.split(','); | |
48 | var link_array = new Array(); | |
49 | ||
50 | for (var j = 0; j < hrpids.length; ++j) { | |
e9cd8d9c | 51 | link_array[j] = '<a href="profile/' + hrpids[j] + '" onclick="window.open(this.href' + parameters + '); return false;">' + names[j] + '</a>'; |
dcc9c952 SJ |
52 | } |
53 | var link = link_array.join('<br />'); | |
54 | } else { | |
e9cd8d9c | 55 | var link = '<a href="profile/' + dots[i].hrpid + '" onclick="window.open(this.href' + parameters + '); return false;">' + dots[i].name + '</a>'; |
dcc9c952 SJ |
56 | } |
57 | ||
e9cd8d9c | 58 | var marker = new google.maps.Marker({ |
dcc9c952 | 59 | 'position': latLng, |
e9cd8d9c SJ |
60 | 'map': map, |
61 | 'title': dots[i].name | |
dcc9c952 | 62 | }); |
ae1938f6 SJ |
63 | marker.bindTo('icon', new ColoredIcon(color)); |
64 | marker.set('color', color); | |
e9cd8d9c SJ |
65 | marker.set('html', link); |
66 | google.maps.event.addListener(marker, 'click', function() { | |
67 | info_window.setContent(this.html); | |
68 | info_window.open(map, this); | |
69 | }); | |
dbbae4f7 SJ |
70 | markers.push(marker); |
71 | } | |
656132ec | 72 | var mc = new MarkerClusterer(map, markers, {'averageCenter': true}); |
dbbae4f7 | 73 | }); |
45dcbe4d SJ |
74 | } |
75 | ||
ae1938f6 SJ |
76 | function ColoredIcon(color) |
77 | { | |
78 | this.set('starcolor', null); | |
79 | this.set('color', color); | |
80 | this.set('icon', 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=|' + color); | |
81 | } | |
82 | ||
83 | ColoredIcon.prototype = new google.maps.MVCObject(); | |
84 | ||
85 | var colors = new Array(); | |
86 | colors['red'] = 'ff0000'; | |
87 | colors['yellow'] = 'ffff00'; | |
88 | colors['blue'] = '0000ff'; | |
89 | colors['green'] = '00ff00'; | |
90 | colors['gray'] = '606060'; | |
91 | ||
92 | function promos_to_color(promos) | |
93 | { | |
94 | var promos_array = promos.split(','); | |
95 | var length = promos_array.length; | |
96 | ||
97 | if (length == 1) { | |
98 | return colors[promo_to_color(promos)]; | |
99 | } | |
100 | ||
101 | var color_array = new Array(); | |
102 | for (var i = 0; i < length; ++i) { | |
103 | color_array[i] = promo_to_color(promos_array[i]); | |
104 | } | |
105 | ||
106 | return color_average(color_array); | |
107 | } | |
108 | ||
109 | function promo_to_color(promo) | |
110 | { | |
111 | var main_education = promo.charAt(0); | |
112 | ||
113 | switch (main_education) | |
114 | { | |
115 | case 'X': | |
116 | var year_promo = promo.substr(1); | |
117 | if ((year_promo % 2) == 0) { | |
118 | return 'red'; | |
119 | } else { | |
120 | return 'yellow'; | |
121 | } | |
122 | case 'M': | |
123 | return 'green'; | |
124 | case 'D': | |
125 | return 'blue'; | |
126 | default: | |
127 | return 'gray'; | |
128 | } | |
129 | } | |
130 | ||
131 | function color_average(color_array) | |
132 | { | |
133 | var length = color_array.length; | |
134 | var rbg = new Array(0, 0, 0); | |
135 | ||
136 | for (var i = 0; i < length; ++i) { | |
137 | switch (color_array[i]) | |
138 | { | |
139 | case 'red': | |
140 | rbg[0] += 1; | |
141 | break; | |
142 | case 'yellow': | |
143 | rbg[0] += 1; | |
144 | rbg[1] += 1; | |
145 | break; | |
146 | case 'blue': | |
147 | rbg[1] += 1; | |
148 | break; | |
149 | case 'green': | |
150 | rbg[2] += 1; | |
151 | break; | |
152 | case 'gray': | |
153 | rbg[0] += 0.5; | |
154 | rbg[1] += 0.5; | |
155 | rbg[2] += 0.5; | |
156 | break; | |
157 | default: | |
158 | break; | |
159 | } | |
160 | } | |
161 | ||
162 | var color_code = ''; | |
163 | for (var i = 0; i < 3; ++i) { | |
164 | var color = Math.floor(rbg[i] / length * 255); | |
165 | var hexa = color.toString(16); | |
166 | if (hexa.length == 1) { | |
167 | hexa += '0'; | |
168 | } | |
169 | color_code += hexa; | |
170 | } | |
171 | ||
172 | return color_code; | |
173 | } | |
174 | ||
45dcbe4d | 175 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |