Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 3.18 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. var centerLatitude = 42.365338;
  2. var centerLongitude = -71.0568232;
  3. var startZoom = 10;
  4. //var map;
  5. var clusterer;
  6. var do_refresh = true;
  7.  
  8. function init() {
  9.     if (GBrowserIsCompatible()) {
  10.         map = new GMap2(document.getElementById("map"));
  11.         map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
  12.         map.addControl(new GLargeMapControl());
  13.         map.addControl(new GScaleControl());
  14.         map.addControl(new GMapTypeControl());
  15.         GEvent.addListener(map,'zoomend',function(oldLevel, newLevel) {
  16.             // zooming requires this: remove the existing points
  17.             map.clearOverlays();
  18.             updateMarkers();
  19.         });
  20.  
  21.         GEvent.addListener(map,'moveend',function() {
  22.             updateMarkers();
  23.         });
  24.         clusterer = new Clusterer(map);
  25.         setTimeout(updateMarkers, 1000, true);
  26.     }
  27. }
  28.  
  29. function createMarker(gpoint,infoWinStr) {
  30.      var marker = new GMarker(gpoint);
  31.      GEvent.addListener(marker, 'click', function() {
  32.          // var markerHTML = (gpoint.lat() + ", " + gpoint.lng());
  33.           do_refresh = false;
  34.           marker.openExtInfoWindow(
  35.           map,
  36.           "simple_example_window",
  37.           infoWinStr,
  38.           {beakOffset: 3}
  39.             );
  40.           //GEvent.trigger(marker,'click');
  41.           setTimeout(function() {
  42.             do_refresh = true;
  43.           }, 5000);
  44.     });
  45.     return marker;
  46. }
  47.  
  48. function updateMarkers() {
  49.     if (!do_refresh) return;
  50.     //create the boundary for the data
  51.     var clusterer = new Clusterer(map);
  52.     var bounds = map.getBounds();
  53.     var southWest = bounds.getSouthWest();
  54.     var northEast = bounds.getNorthEast();
  55.     var url = '/rep_jay/fc_in_bounds?ne=' + northEast.toUrlValue() +
  56.                   '&sw=' + southWest.toUrlValue();
  57.        //  GLog.writeUrl(url);
  58.     //retrieve the points using Ajax
  59.     var request = GXmlHttp.create();
  60.     request.open('GET', url, true);
  61.     request.onreadystatechange = function() {
  62.          if (request.readyState == 4) {
  63.             if (request.status != 200) {
  64.               GLog.write("status: " + (request.status || "?"));
  65.             } else {
  66.                 var data = request.responseText;
  67.                 var edata = eval("(" + data + ")");
  68.  
  69.                 //remove the existing points
  70.                 map.clearOverlays();
  71.                 var points = edata.result;
  72.  
  73.                 //create each point from the list
  74.                 for (var i = 0; i < points.length; i++) {
  75.                     var gp = new GLatLng(points[i].latitude + (Math.random()-.5)/10000, points[i].longitude + (Math.random()-.5)/10000);
  76.  
  77.                     var marker = createMarker(gp,'<div class="title">'+points[i]['first_name']+' '+points[i]['last_name']+'</div><div class="address">'+points[i]['address']+' <br> '+points[i]['date']+' '+points[i]['amount']+'</div>');
  78.                     var donors = points[i].first_name+' '+points[i].last_name+': '+points[i].amount;
  79.                     clusterer.AddMarker(marker, donors);
  80.                 }
  81.             }
  82.          }
  83.     }
  84.     request.send(null);
  85. }
  86.  
  87. window.onload = init;
  88. window.onunload = function() {
  89.   // unloaded = true;
  90.   GUnload();
  91. };