Commit fac2a4178da6f497b0108e925e8ecd4793026572

Authored by Edmar Moretti
1 parent 92822439
Exists in master

Correção no uso do plugin heatmap

Showing 1 changed file with 29 additions and 18 deletions   Show diff stats
pacotes/heatmap/src/heatmap-openlayers.js
1 -/* 1 +/*
2 * heatmap.js OpenLayers Heatmap Class 2 * heatmap.js OpenLayers Heatmap Class
3 * 3 *
4 * Copyright (c) 2011, Patrick Wied (http://www.patrick-wied.at) 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) 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. 6 * and the Beerware (http://en.wikipedia.org/wiki/Beerware) license.
7 - * 7 + *
8 * Modified on Jun,06 2011 by Antonio Santiago (http://www.acuriousanimal.com) 8 * Modified on Jun,06 2011 by Antonio Santiago (http://www.acuriousanimal.com)
9 * - Heatmaps as independent map layer. 9 * - Heatmaps as independent map layer.
10 * - Points based on OpenLayers.LonLat. 10 * - Points based on OpenLayers.LonLat.
11 * - Data initialization in constructor. 11 * - Data initialization in constructor.
12 * - Improved 'addDataPoint' to add new lonlat based points. 12 * - Improved 'addDataPoint' to add new lonlat based points.
13 - */ 13 + */
14 OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, { 14 OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, {
15 // the heatmap isn't a basic layer by default - you usually want to display the heatmap over another map ;) 15 // the heatmap isn't a basic layer by default - you usually want to display the heatmap over another map ;)
16 isBaseLayer: false, 16 isBaseLayer: false,
@@ -34,18 +34,18 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, { @@ -34,18 +34,18 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, {
34 // create the heatmap with passed heatmap-options 34 // create the heatmap with passed heatmap-options
35 this.heatmap = h337.create(hmoptions); 35 this.heatmap = h337.create(hmoptions);
36 36
37 - handler = function(){ 37 + handler = function(){
38 if(this.map && this.tmpData.max){ 38 if(this.map && this.tmpData.max){
39 - this.updateLayer(); 39 + this.updateLayer();
40 } 40 }
41 }; 41 };
42 - handler1 = function(){ 42 + handler1 = function(){
43 if(this.tmpData && this.tmpData.max){ 43 if(this.tmpData && this.tmpData.max){
44 this.toggle(); 44 this.toggle();
45 this.updateLayer(); 45 this.updateLayer();
46 } 46 }
47 }; 47 };
48 - 48 +
49 // on zoomend and moveend we have to move the canvas element and redraw the datapoints with new positions 49 // on zoomend and moveend we have to move the canvas element and redraw the datapoints with new positions
50 //map.events.register("zoomend", this, handler); 50 //map.events.register("zoomend", this, handler);
51 map.events.register("moveend", this, handler); 51 map.events.register("moveend", this, handler);
@@ -53,16 +53,16 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, { @@ -53,16 +53,16 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, {
53 map.events.register( 53 map.events.register(
54 "removed", 54 "removed",
55 this, 55 this,
56 - function(){ 56 + function(){
57 if(this.tmpData.max){ 57 if(this.tmpData.max){
58 - this.destroy(); 58 + this.destroy();
59 } 59 }
60 }); 60 });
61 }, 61 },
62 updateLayer: function(){ 62 updateLayer: function(){
63 var pixelOffset = this.getPixelOffset(), 63 var pixelOffset = this.getPixelOffset(),
64 el = this.heatmap.get('element'); 64 el = this.heatmap.get('element');
65 - // if the pixeloffset e.g. for x was positive move the canvas element to the left by setting left:-offset.y px 65 + // if the pixeloffset e.g. for x was positive move the canvas element to the left by setting left:-offset.y px
66 // otherwise move it the right by setting it a positive value. same for top 66 // otherwise move it the right by setting it a positive value. same for top
67 el.style.top = ((pixelOffset.y > 0)?('-'+pixelOffset.y):(Math.abs(pixelOffset.y)))+'px'; 67 el.style.top = ((pixelOffset.y > 0)?('-'+pixelOffset.y):(Math.abs(pixelOffset.y)))+'px';
68 el.style.left = ((pixelOffset.x > 0)?('-'+pixelOffset.x):(Math.abs(pixelOffset.x)))+'px'; 68 el.style.left = ((pixelOffset.x > 0)?('-'+pixelOffset.x):(Math.abs(pixelOffset.x)))+'px';
@@ -80,9 +80,9 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, { @@ -80,9 +80,9 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, {
80 c_lonlat = new OpenLayers.LonLat(c.lon, c.lat), 80 c_lonlat = new OpenLayers.LonLat(c.lon, c.lat),
81 c_pixel = this.mapLayer.getViewPortPxFromLonLat(c_lonlat); 81 c_pixel = this.mapLayer.getViewPortPxFromLonLat(c_lonlat);
82 82
83 - return { 83 + return {
84 x: o_pixel.x - c_pixel.x, 84 x: o_pixel.x - c_pixel.x,
85 - y: o_pixel.y - c_pixel.y 85 + y: o_pixel.y - c_pixel.y
86 }; 86 };
87 87
88 }, 88 },
@@ -90,20 +90,31 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, { @@ -90,20 +90,31 @@ OpenLayers.Layer.Heatmap = OpenLayers.Class(OpenLayers.Layer, {
90 var set = {}, 90 var set = {},
91 dataset = obj.data, 91 dataset = obj.data,
92 dlen = dataset.length, 92 dlen = dataset.length,
93 - entry, lonlat, pixel; 93 + entry, lonlat, pixel, proj, tproj, vp;
94 94
95 set.max = obj.max; 95 set.max = obj.max;
96 set.data = []; 96 set.data = [];
  97 +
97 // get the pixels for all the lonlat entries 98 // get the pixels for all the lonlat entries
  99 + proj = this.map.getProjectionObject();
  100 + tproj = this.projection;
98 while(dlen--){ 101 while(dlen--){
99 - entry = dataset[dlen],  
100 - lonlat = entry.lonlat.clone().transform(this.projection, this.map.getProjectionObject()),  
101 - pixel = this.roundPixels(this.getViewPortPxFromLonLat(lonlat)); 102 + entry = dataset[dlen];
  103 + try{
  104 + lonlat = entry.lonlat.clone().transform(tproj, proj);
  105 + if(lonlat){
  106 + vp = this.getViewPortPxFromLonLat(lonlat);
102 107
103 - if(pixel){  
104 - set.data.push({x: pixel.x, y: pixel.y, count: entry.count}); 108 + pixel = this.roundPixels(vp);
  109 +
  110 + if(pixel){
  111 + set.data.push({x: pixel.x, y: pixel.y, count: entry.count});
  112 + }
  113 + }
105 } 114 }
  115 + catch(e){}
106 } 116 }
  117 +
107 this.tmpData = obj; 118 this.tmpData = obj;
108 this.heatmap.store.setDataSet(set); 119 this.heatmap.store.setDataSet(set);
109 }, 120 },