PageRenderTime 233ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/src/heatmap-gmaps.js

http://github.com/pa7/heatmap.js
JavaScript | 163 lines | 115 code | 37 blank | 11 comment | 3 complexity | 335deb67536f586b22ac3284f0e1c37a MD5 | raw file
  1. /*
  2. * heatmap.js GMaps overlay
  3. *
  4. * Copyright (c) 2011, Patrick Wied (http://www.patrick-wied.at)
  5. * Dual-licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  6. * and the Beerware (http://en.wikipedia.org/wiki/Beerware) license.
  7. */
  8. function HeatmapOverlay(map, cfg){
  9. var me = this;
  10. me.heatmap = null;
  11. me.conf = cfg;
  12. me.latlngs = [];
  13. me.bounds = null;
  14. me.setMap(map);
  15. google.maps.event.addListener(map, 'idle', function() { me.draw() });
  16. }
  17. HeatmapOverlay.prototype = new google.maps.OverlayView();
  18. HeatmapOverlay.prototype.onAdd = function(){
  19. var panes = this.getPanes(),
  20. w = this.getMap().getDiv().clientWidth,
  21. h = this.getMap().getDiv().clientHeight,
  22. el = document.createElement("div");
  23. el.style.position = "absolute";
  24. el.style.top = 0;
  25. el.style.left = 0;
  26. el.style.width = w + "px";
  27. el.style.height = h + "px";
  28. el.style.border = 0;
  29. this.conf.element = el;
  30. panes.overlayLayer.appendChild(el);
  31. this.heatmap = h337.create(this.conf);
  32. }
  33. HeatmapOverlay.prototype.onRemove = function(){
  34. // Empty for now.
  35. }
  36. HeatmapOverlay.prototype.draw = function(){
  37. var overlayProjection = this.getProjection(),
  38. currentBounds = this.map.getBounds();
  39. if (currentBounds.equals(this.bounds)) {
  40. return;
  41. }
  42. this.bounds = currentBounds;
  43. var ne = overlayProjection.fromLatLngToDivPixel(currentBounds.getNorthEast()),
  44. sw = overlayProjection.fromLatLngToDivPixel(currentBounds.getSouthWest()),
  45. topY = ne.y,
  46. leftX = sw.x,
  47. h = sw.y - ne.y,
  48. w = ne.x - sw.x;
  49. this.conf.element.style.left = leftX + 'px';
  50. this.conf.element.style.top = topY + 'px';
  51. this.conf.element.style.width = w + 'px';
  52. this.conf.element.style.height = h + 'px';
  53. this.heatmap.store.get("heatmap").resize();
  54. if(this.latlngs.length > 0){
  55. this.heatmap.clear();
  56. var len = this.latlngs.length,
  57. projection = this.getProjection();
  58. d = {
  59. max: this.heatmap.store.max,
  60. data: []
  61. };
  62. while(len--){
  63. var latlng = this.latlngs[len].latlng;
  64. if(!currentBounds.contains(latlng)) { continue; }
  65. // DivPixel is pixel in overlay pixel coordinates... we need
  66. // to transform to screen coordinates so it'll match the canvas
  67. // which is continually repositioned to follow the screen.
  68. var divPixel = projection.fromLatLngToDivPixel(latlng),
  69. screenPixel = new google.maps.Point(divPixel.x - leftX, divPixel.y - topY);
  70. var roundedPoint = this.pixelTransform(screenPixel);
  71. d.data.push({
  72. x: roundedPoint.x,
  73. y: roundedPoint.y,
  74. count: this.latlngs[len].c
  75. });
  76. }
  77. this.heatmap.store.setDataSet(d);
  78. }
  79. }
  80. HeatmapOverlay.prototype.pixelTransform = function(p){
  81. var w = this.heatmap.get("width"),
  82. h = this.heatmap.get("height");
  83. while(p.x < 0){
  84. p.x+=w;
  85. }
  86. while(p.x > w){
  87. p.x-=w;
  88. }
  89. while(p.y < 0){
  90. p.y+=h;
  91. }
  92. while(p.y > h){
  93. p.y-=h;
  94. }
  95. p.x = (p.x >> 0);
  96. p.y = (p.y >> 0);
  97. return p;
  98. }
  99. HeatmapOverlay.prototype.setDataSet = function(data){
  100. var mapdata = {
  101. max: data.max,
  102. data: []
  103. };
  104. var d = data.data,
  105. dlen = d.length,
  106. projection = this.getProjection();
  107. this.latlngs = [];
  108. while(dlen--){
  109. var latlng = new google.maps.LatLng(d[dlen].lat, d[dlen].lng);
  110. this.latlngs.push({latlng: latlng, c: d[dlen].count});
  111. var point = this.pixelTransform(projection.fromLatLngToDivPixel(latlng));
  112. mapdata.data.push({x: point.x, y: point.y, count: d[dlen].count});
  113. }
  114. this.heatmap.clear();
  115. this.heatmap.store.setDataSet(mapdata);
  116. }
  117. HeatmapOverlay.prototype.addDataPoint = function(lat, lng, count){
  118. var projection = this.getProjection(),
  119. latlng = new google.maps.LatLng(lat, lng),
  120. point = this.pixelTransform(projection.fromLatLngToDivPixel(latlng));
  121. this.heatmap.store.addDataPoint(point.x, point.y, count);
  122. this.latlngs.push({ latlng: latlng, c: count });
  123. }
  124. HeatmapOverlay.prototype.toggle = function(){
  125. this.heatmap.toggleDisplay();
  126. }