app/soc/content/js/map-090420.js
changeset 2253 ab216123bee7
parent 2251 2d286b2419d1
child 2390 723dfa4811e8
equal deleted inserted replaced
2252:947cfb05463b 2253:ab216123bee7
   171       });
   171       });
   172     }
   172     }
   173   }
   173   }
   174 };
   174 };
   175 
   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   // Geocoder object for obtaining locations from city/country
       
   184   var geocoder = new GClientGeocoder();
       
   185   
       
   186   // Lat/lng pairs stored for drawing polylines.
       
   187   var student_lat = 0;
       
   188   var student_lng = 0;
       
   189   var mentor_lat = 0;
       
   190   var mentor_lng = 0;
       
   191 
       
   192   // Setup required icons
       
   193   var base_icon = new GIcon();
       
   194   base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
       
   195   base_icon.iconSize = new GSize(20, 34);
       
   196   base_icon.shadowSize = new GSize(37, 34);
       
   197   base_icon.iconAnchor = new GPoint(9, 34);
       
   198   base_icon.infoWindowAnchor = new GPoint(9, 2);
       
   199   base_icon.infoShadowAnchor = new GPoint(18, 25);
       
   200   var student_icon = new GIcon(base_icon);
       
   201   student_icon.image = "http://www.google.com/mapfiles/marker.png";
       
   202   var mentor_icon = new GIcon(base_icon);
       
   203   mentor_icon.image = "/soc/content/images/mentor-marker.png";
       
   204 
       
   205   // Mark mentor marker if he has published the location and call 
       
   206   // drawStudentMarker function.
       
   207   function drawMarkers(map_elem) {
       
   208     function iterateStudents(students, mentor_name, mentor_published) {
       
   209       for (student in students) {
       
   210         drawStudentMarker(students[student], mentor_name, mentor_published);
       
   211       }
       
   212     }
       
   213     if (map_elem.type == 'mentor') {
       
   214       geocoder.setBaseCountryCode(map_elem.ccTLD);
       
   215       geocoder.getLatLng(
       
   216         map_elem.city,
       
   217         function(point) {
       
   218           if (point) {
       
   219             var marker = new GMarker(point, mentor_icon);
       
   220             mentor_lat = marker.getPoint().lat();
       
   221             mentor_lng = marker.getPoint().lng();
       
   222             var html = "<strong>" + map_elem.name + "</strong><br> Mentor";
       
   223             GEvent.addListener(marker, "click", function() {
       
   224                  marker.openInfoWindowHtml(html);
       
   225             });
       
   226             map.addOverlay(marker);
       
   227           } else {
       
   228             mentor_lat = null;
       
   229             mentor_lng = null;
       
   230           }
       
   231           iterateStudents(map_elem.students, map_elem.name, true);
       
   232         }
       
   233       );
       
   234     } else if (map_elem.type == 'none') {
       
   235       drawStudentMarker(map_elem.student, map_elem.name, false);
       
   236     }
       
   237   }
       
   238 
       
   239   // Mark student and enable a popup box upon click
       
   240   function drawStudentMarker(student, mname, mentor_published) {
       
   241     geocoder.setBaseCountryCode(student.ccTLD);
       
   242     geocoder.getLatLng(
       
   243       student.city,
       
   244       function(point) {
       
   245         if (point) {
       
   246           var marker = new GMarker(point, student_icon);
       
   247           var html = "<strong>" + student.name + "</strong><br>";
       
   248           html += "<a href='"+ student.url + "'>" + student.summary + "</a><br>";
       
   249           html += "Mentor: " + mname;
       
   250           GEvent.addListener(marker, "click", function() {
       
   251             marker.openInfoWindowHtml(html);
       
   252           });
       
   253           student_lat = marker.getPoint().lat();
       
   254           student_lng = marker.getPoint().lng();
       
   255           map.addOverlay(marker);
       
   256         } else {
       
   257           student_lat = null;
       
   258           student_lng = null;
       
   259         }
       
   260         if (mentor_published) {
       
   261           drawPolyLine();
       
   262         }
       
   263       }
       
   264     );
       
   265   }
       
   266 
       
   267   // Draw a polyline between the student and his mentor
       
   268   drawPolyLine = function() {
       
   269     var polyline = new GPolyline([
       
   270       new GLatLng(mentor_lat, mentor_lng),
       
   271       new GLatLng(student_lat, student_lng)
       
   272       ], "#ff0000", 3);
       
   273     map.addOverlay(polyline);
       
   274   }
       
   275     
       
   276   // Map load function
       
   277   this.map_load = function(map_data) {
       
   278     // Check if browser is gmap compatible.
       
   279     if (GBrowserIsCompatible()) {
       
   280       // Create the map and add small controls
       
   281       map = new GMap2(document.getElementById(map_div));
       
   282       map.addControl(new GLargeMapControl());
       
   283         map.addControl(new GMapTypeControl());
       
   284       
       
   285       // Set map center and initial zoom level
       
   286       map.setCenter(new GLatLng(0, 0), 1);
       
   287       
       
   288       for (elem in map_data) {
       
   289         drawMarkers(map_data[elem])
       
   290       }
       
   291     }
       
   292   }
       
   293 };