|
1 role_profile_gmap = new function(){ |
|
2 // Create global variables |
|
3 var map; |
|
4 var marker; |
|
5 var geocoder; |
|
6 |
|
7 // The following strings can be customized to reflect ids in the page. |
|
8 // You can also add or remove fields used for GMap Geocoding in |
|
9 // the JSON address object |
|
10 |
|
11 var current_lat = 0; |
|
12 var current_lng = 0; |
|
13 |
|
14 // Two different levels for zoom: Starting one and an inner that |
|
15 // is used when showing the map if lat and lon page fields are set |
|
16 var world_zoom = 0; |
|
17 var country_zoom = 4; |
|
18 var state_zoom = 6; |
|
19 var city_zoom = 10; |
|
20 var address_zoom = 13; |
|
21 |
|
22 // Do not add a starting # as this JQuery selector seems |
|
23 // incompatible with GMap API |
|
24 var map_div = "role_profile_map"; |
|
25 |
|
26 var field_lat = "#id_latitude"; |
|
27 var field_lng = "#id_longitude"; |
|
28 // Need to save old values to avoid unwanted updating |
|
29 // of lat and lot if marker dragged and blur another time an address field |
|
30 var address = { |
|
31 street: { |
|
32 id: "#id_res_street", |
|
33 old_value: "" |
|
34 }, |
|
35 city: { |
|
36 id: "#id_res_city", |
|
37 old_value: "" |
|
38 }, |
|
39 state: { |
|
40 id: "#id_res_state", |
|
41 old_value: "" |
|
42 }, |
|
43 country: { |
|
44 id: "#id_res_country", |
|
45 old_value: "" |
|
46 }, |
|
47 postalcode: { |
|
48 id: "#id_res_postalcode", |
|
49 old_value: "" |
|
50 } |
|
51 } |
|
52 |
|
53 // Save current address fields in the JSON Object |
|
54 function saveOldAddress() { |
|
55 for (var a in address) { |
|
56 address[a].old_value = $(address[a].id).val(); |
|
57 } |
|
58 } |
|
59 |
|
60 // Return true if the user has edited address fields |
|
61 function isNewAddress() { |
|
62 for (var a in address) { |
|
63 if ($(address[a].id).val() != address[a].old_value) return true; |
|
64 } |
|
65 return false; |
|
66 } |
|
67 |
|
68 // Write saved lat and lng values to page fields |
|
69 function setLatLngFields() { |
|
70 $(field_lat).val(current_lat); |
|
71 $(field_lng).val(current_lng); |
|
72 } |
|
73 |
|
74 // Read lat and lng fields and store them |
|
75 function readLatLngFields() { |
|
76 current_lat = $(field_lat).val(); |
|
77 current_lng = $(field_lng).val(); |
|
78 } |
|
79 |
|
80 // This function reads address fields, merge them and uses |
|
81 // GMap API geocoding to find the first hit |
|
82 // Using geocoding http://code.google.com/intl/it-IT/apis/maps/documentation/services.html#Geocoding |
|
83 function calculateAddress() { |
|
84 // If the user has really edited address fields... |
|
85 if (isNewAddress()) { |
|
86 // Merge address fields |
|
87 var address_string = ""; |
|
88 for (var a in address) { |
|
89 address_string+=$(address[a].id).val(); |
|
90 if (a!=address.length-1) {address_string+=","}; |
|
91 } |
|
92 |
|
93 // Ask GMap API for geocoding |
|
94 geocoder.getLatLng( |
|
95 address_string, |
|
96 function(point) { |
|
97 // If a point is found |
|
98 if (point) { |
|
99 // Save the current address in the JSON object |
|
100 saveOldAddress(); |
|
101 // Set the new zoom, map center and marker coords |
|
102 var zoom_set = world_zoom; |
|
103 if ($(address.street.id).val()!="") zoom_set = address_zoom; |
|
104 else if ($(address.city.id).val()!="") zoom_set = city_zoom; |
|
105 else if ($(address.state.id).val()!="") zoom_set = state_zoom; |
|
106 else if ($(address.country.id).val()!="") zoom_set = country_zoom; |
|
107 map.setCenter(point, zoom_set); |
|
108 marker.setPoint(point); |
|
109 map.clearOverlays(); |
|
110 map.addOverlay(marker); |
|
111 // Save point coords in local variables and then update |
|
112 // the page lat/lng fields |
|
113 current_lat = point.lat(); |
|
114 current_lng = point.lng(); |
|
115 setLatLngFields(); |
|
116 } |
|
117 } |
|
118 ); |
|
119 } |
|
120 } |
|
121 |
|
122 // Public function to load the map |
|
123 this.map_load = function() { |
|
124 // All can happen only if there is gmap compatible browser. |
|
125 // TODO: Fallback in case the browser is not compatible |
|
126 if (GBrowserIsCompatible()) { |
|
127 // Save the address fields. This is useful if the page is being edited |
|
128 // to not update blindly the lat/lng fields with GMap geocoding if |
|
129 // blurring an address field |
|
130 saveOldAddress(); |
|
131 var starting_point; |
|
132 var zoom_selected = world_zoom; |
|
133 var show_marker = true; |
|
134 |
|
135 // Create the map and add small controls |
|
136 map = new GMap2(document.getElementById(map_div)); |
|
137 map.addControl(new GSmallMapControl()); |
|
138 map.addControl(new GMapTypeControl()); |
|
139 |
|
140 // Instantiate a global geocoder for future use |
|
141 geocoder = new GClientGeocoder(); |
|
142 |
|
143 // If lat and lng fields are not void (the page is being edited) then |
|
144 // update the starting coords, modify the zoom level and tells following |
|
145 // code to show the marker |
|
146 if ($(field_lat).val()!="" && $(field_lng).val()!="") { |
|
147 readLatLngFields(); |
|
148 zoom_selected = address_zoom; |
|
149 show_marker = true; |
|
150 } |
|
151 |
|
152 // Set map center, marker coords and show it if this is an editing |
|
153 starting_point = new GLatLng(current_lat,current_lng); |
|
154 map.setCenter(starting_point, zoom_selected); |
|
155 marker = new GMarker(starting_point, {draggable:true}); |
|
156 if (show_marker) map.addOverlay(marker); |
|
157 |
|
158 // Adds a new event listener to geocode the address when an address |
|
159 // field is blurred |
|
160 for (var a in address) { |
|
161 $(address[a].id).blur(calculateAddress); |
|
162 } |
|
163 |
|
164 // Adds a new event listener: if the marker has been dragged around... |
|
165 GEvent.addListener(marker, "dragend", function() { |
|
166 // Update internal variables with current marker coords... |
|
167 current_lat = marker.getPoint().lat(); |
|
168 current_lng = marker.getPoint().lng(); |
|
169 // ...and set page fields accordingly |
|
170 setLatLngFields(); |
|
171 }); |
|
172 } |
|
173 } |
|
174 }; |
|
175 |
|
176 org_home_gmap = new function(){ |
|
177 // Global variables |
|
178 var map; |
|
179 |
|
180 // HTML div tag where map needs to be inserted |
|
181 var map_div = "org_home_map"; |
|
182 |
|
183 // Setup required icons |
|
184 var base_icon = new GIcon(); |
|
185 base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png"; |
|
186 base_icon.iconSize = new GSize(20, 34); |
|
187 base_icon.shadowSize = new GSize(37, 34); |
|
188 base_icon.iconAnchor = new GPoint(9, 34); |
|
189 base_icon.infoWindowAnchor = new GPoint(9, 2); |
|
190 base_icon.infoShadowAnchor = new GPoint(18, 25); |
|
191 var student_icon = new GIcon(base_icon); |
|
192 student_icon.image = "http://www.google.com/mapfiles/marker.png"; |
|
193 var mentor_icon = new GIcon(base_icon); |
|
194 mentor_icon.image = "/soc/content/images/mentor-marker.png"; |
|
195 |
|
196 // Map load function |
|
197 this.map_load = function(map_data) { |
|
198 |
|
199 if (GBrowserIsCompatible()) { |
|
200 // Create the map and add small controls |
|
201 map = new GMap2(document.getElementById(map_div)); |
|
202 map.addControl(new GLargeMapControl()); |
|
203 map.addControl(new GMapTypeControl()); |
|
204 |
|
205 // Set map center and initial zoom level |
|
206 map.setCenter(new GLatLng(0, 0), 1); |
|
207 |
|
208 var mentors = {}; |
|
209 var students = {}; |
|
210 var projects = {}; |
|
211 var polylines = []; |
|
212 |
|
213 jQuery.each(map_data.people, function(key, person) { |
|
214 if (person.type === "student") { |
|
215 students[key] = { |
|
216 "name": person.name, |
|
217 "lat": person.lat, |
|
218 "long": person.long, |
|
219 "projects": person.projects |
|
220 } |
|
221 } |
|
222 if (person.type === "mentor") { |
|
223 mentors[key] = { |
|
224 "name": person.name, |
|
225 "lat": person.lat, |
|
226 "long": person.long, |
|
227 "projects": person.projects |
|
228 }; |
|
229 } |
|
230 }); |
|
231 |
|
232 // Iterate over projects to draw polylines |
|
233 jQuery.each(map_data.projects, function (key, project) { |
|
234 var current_student = students[project.student_key]; |
|
235 var current_mentor = mentors[project.mentor_key]; |
|
236 if (current_student !== undefined && |
|
237 current_mentor !== undefined && |
|
238 current_student.lat !== null && |
|
239 current_student.long !== null && |
|
240 current_mentor.lat !== null && |
|
241 current_mentor.long !== null) { |
|
242 polylines.push([[current_student.lat,current_student.long],[current_mentor.lat,current_mentor.long]]); |
|
243 } |
|
244 }); |
|
245 |
|
246 // Iterate over students |
|
247 jQuery.each(students, function(key, person) { |
|
248 var html = ""; |
|
249 var marker = null; |
|
250 |
|
251 if (person.lat!==null && person.long!==null) { |
|
252 point = new GLatLng(person.lat, |
|
253 person.long); |
|
254 |
|
255 marker = new GMarker(point, student_icon); |
|
256 html = "<strong>" + person.name + "</strong><br />"; |
|
257 html += "<span style='font-style:italic;'>Student</span><br />"; |
|
258 html += "<div style='height:100px;width:300px;overflow:auto;font-size:70%'>"; |
|
259 // iterate through projects |
|
260 jQuery.each(person.projects, function () { |
|
261 var current_project = map_data.projects[this]; |
|
262 html += "<a href='"+ current_project.redirect + "'>" + current_project.title + "</a><br />"; |
|
263 html += "Mentor: " + current_project.mentor_name + "<br />"; |
|
264 }); |
|
265 html+= "</div>"; |
|
266 GEvent.addListener(marker, "click", function() { |
|
267 marker.openInfoWindowHtml(html); |
|
268 }); |
|
269 |
|
270 map.addOverlay(marker); |
|
271 } |
|
272 }); |
|
273 |
|
274 // Iterate over mentors |
|
275 jQuery.each(mentors, function(key, person) { |
|
276 var html = ""; |
|
277 var marker = null; |
|
278 |
|
279 if (person.lat!==null && person.long!==null) { |
|
280 point = new GLatLng(person.lat, |
|
281 person.long); |
|
282 |
|
283 marker = new GMarker(point, mentor_icon); |
|
284 html = "<strong>" + person.name + "</strong><br />"; |
|
285 html += "<span style='font-style:italic;'>Student</span><br />"; |
|
286 html += "<div style='height:100px;width:300px;overflow:auto;font-size:70%'>"; |
|
287 // iterate through projects |
|
288 jQuery.each(person.projects, function () { |
|
289 var current_project = map_data.projects[this]; |
|
290 html += "<a href='"+ current_project.redirect + "'>" + current_project.title + "</a><br />"; |
|
291 html += "Student: " + current_project.student_name + "<br />"; |
|
292 }); |
|
293 html+= "</div>"; |
|
294 |
|
295 GEvent.addListener(marker, "click", function() { |
|
296 marker.openInfoWindowHtml(html); |
|
297 }); |
|
298 |
|
299 map.addOverlay(marker); |
|
300 } |
|
301 }); |
|
302 |
|
303 // Draw all polylines |
|
304 jQuery.each(polylines, function() { |
|
305 var from = new GLatLng(this[0][0],this[0][1]); |
|
306 var to = new GLatLng(this[1][0],this[1][1]); |
|
307 var polyline = new GPolyline([from, to], "#ff0000", 3); |
|
308 map.addOverlay(polyline); |
|
309 }); |
|
310 } |
|
311 } |
|
312 }; |