1 | function PwgDataLoader( map, opts ) |
---|
2 | { |
---|
3 | this._map = map; |
---|
4 | this.options = Object.extend( |
---|
5 | { |
---|
6 | reload_data_timeout: 1800, |
---|
7 | rectangle_of_confusion: G_DEFAULT_ICON.iconSize |
---|
8 | } |
---|
9 | , opts || {} ); |
---|
10 | this.options.rectangle_of_confusion = new GSize( this.options.rectangle_of_confusion.width, this.options.rectangle_of_confusion.height ); |
---|
11 | if (this.options.rectangle_of_confusion.width<16) this.options.rectangle_of_confusion.width=16; |
---|
12 | if (this.options.rectangle_of_confusion.height<16) this.options.rectangle_of_confusion.height=16; |
---|
13 | } |
---|
14 | |
---|
15 | |
---|
16 | // returns the number of digits (between 0 and 6) to round a lat/lon so that it does not vary by more than factor*pow(-exp) |
---|
17 | getLatLonDigits = function(d, factor, exp) |
---|
18 | { |
---|
19 | if (d<0) return getLatLonDigits(-d,factor,exp); |
---|
20 | var digits = Math.ceil( exp - Math.log(d*factor)/Math.LN10 ); |
---|
21 | return digits<0 ? 0 : (digits>6 ? 6 : digits); |
---|
22 | } |
---|
23 | |
---|
24 | Math.roundN = function(d, digits) { var c = Math.pow(10,digits); return Math.round(d*c)/c; } |
---|
25 | |
---|
26 | |
---|
27 | PwgDataLoader.prototype = { |
---|
28 | |
---|
29 | |
---|
30 | _urlMapData: null, |
---|
31 | _timerReloadData: null, |
---|
32 | _dataLoading: false, |
---|
33 | |
---|
34 | _previousLoadDataReq : {box: null, zoom:0, resultBounds:null}, |
---|
35 | |
---|
36 | start: function(urlMapData) |
---|
37 | { |
---|
38 | this._urlMapData = urlMapData; |
---|
39 | GEvent.bind( this._map, "movestart", this, this.clearTimerReloadData ); |
---|
40 | GEvent.bind( this._map, "moveend", this, this._onMapMoveEnd ); |
---|
41 | this._loadData(); |
---|
42 | }, |
---|
43 | |
---|
44 | terminate: function() |
---|
45 | { |
---|
46 | this.clearTimerReloadData(); |
---|
47 | this._map = null; |
---|
48 | }, |
---|
49 | |
---|
50 | clearTimerReloadData : function() |
---|
51 | { |
---|
52 | if (this._timerReloadData) |
---|
53 | { |
---|
54 | clearTimeout( this._timerReloadData ); |
---|
55 | this._timerReloadData = null; |
---|
56 | return true; |
---|
57 | } |
---|
58 | return false; |
---|
59 | }, |
---|
60 | |
---|
61 | _onMapMoveEnd: function() |
---|
62 | { |
---|
63 | this.clearTimerReloadData(); |
---|
64 | this._timerReloadData = setTimeout( this._onTimeoutLoadData.bind(this), this.options.reload_data_timeout ); |
---|
65 | }, |
---|
66 | |
---|
67 | |
---|
68 | _onTimeoutLoadData: function () |
---|
69 | { |
---|
70 | if (this._dataLoading) return; // still in progress |
---|
71 | this.clearTimerReloadData(); |
---|
72 | this._loadData(); |
---|
73 | }, |
---|
74 | |
---|
75 | _loadData: function() |
---|
76 | { |
---|
77 | var bounds = this._map.getBounds(); |
---|
78 | |
---|
79 | // BEGIN BUG in maps api 2.185 up to "today" 2.193 - when map wraps horizontally more than 360 deg - the getBounds is wrong |
---|
80 | if ( this._map.getZoom() <= 2 || this._map.getZoom() == 3 && this._map.getSize().width > 1000 ) |
---|
81 | { |
---|
82 | bounds = new GLatLngBounds( |
---|
83 | new GLatLng( bounds.getSouthWest().lat(), -180 ), |
---|
84 | new GLatLng( bounds.getNorthEast().lat(), 180 ) |
---|
85 | ); |
---|
86 | } |
---|
87 | // END BUG |
---|
88 | var latRange = bounds.toSpan().lat(); |
---|
89 | var latPrec = latRange * this.options.rectangle_of_confusion.height / this._map.getSize().height; |
---|
90 | |
---|
91 | var lonRange = bounds.toSpan().lng(); |
---|
92 | var lonPrec = ( lonRange>=0 ? lonRange : 360-lonRange )* this.options.rectangle_of_confusion.width / this._map.getSize().width; |
---|
93 | |
---|
94 | if ( this._previousLoadDataReq.box!=null ) |
---|
95 | { // not the first time |
---|
96 | if ( this._previousLoadDataReq.box.containsBounds( bounds ) ) |
---|
97 | { |
---|
98 | if ( this._previousLoadDataReq.resultBounds == null ) |
---|
99 | return; // no images here |
---|
100 | if ( this._map.getZoom() <= this._previousLoadDataReq.zoom ) |
---|
101 | return; |
---|
102 | else |
---|
103 | { |
---|
104 | if ( |
---|
105 | this._map.getZoom() == this._previousLoadDataReq.zoom + 1 |
---|
106 | && this._map.getZoom() < this._map.getBoundsZoomLevel(this._previousLoadDataReq.resultBounds) |
---|
107 | ) |
---|
108 | { |
---|
109 | if (document.is_debug) GLog.write('no load: zoom crt: '+this._map.getZoom()+'; prev: '+this._previousLoadDataReq.zoom+'; target: '+this._map.getBoundsZoomLevel(this._previousLoadDataReq.resultBounds)); |
---|
110 | return; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | var nd=0, sd=0, ed=0, wd=0; |
---|
120 | if ( !bounds.isFullLat() ) |
---|
121 | { |
---|
122 | nd = latRange*12/100; |
---|
123 | sd = latRange*4/100; |
---|
124 | } |
---|
125 | if ( !bounds.isFullLng() ) |
---|
126 | { |
---|
127 | ed = lonRange*9/100; |
---|
128 | wd = lonRange*7/100; |
---|
129 | } |
---|
130 | var digits = Math.max( getLatLonDigits(latRange,4,2), getLatLonDigits(lonRange,4,2) ); |
---|
131 | var box = new GLatLngBounds( |
---|
132 | new GLatLng( Math.roundN(bounds.getSouthWest().lat()-sd,digits), Math.roundN( bounds.getSouthWest().lng()-wd,digits ) ), |
---|
133 | new GLatLng( Math.roundN(bounds.getNorthEast().lat()+nd,digits), Math.roundN( bounds.getNorthEast().lng()+ed,digits) ) |
---|
134 | ); |
---|
135 | |
---|
136 | var url = this._urlMapData; |
---|
137 | url += "&box=" + box.getSouthWest().toUrlValue(5) + "," + box.getNorthEast().toUrlValue(5); |
---|
138 | url += "&lap=" + Math.roundN( latPrec, getLatLonDigits(latPrec,1,1) ).toString(); |
---|
139 | url += "&lop=" + Math.roundN( lonPrec, getLatLonDigits(lonPrec,1,1) ).toString(); |
---|
140 | |
---|
141 | if (document.is_debug) { |
---|
142 | GLog.write( "bounds: " + this._map.getBounds().getSouthWest().toUrlValue() + " " + this._map.getBounds().getNorthEast().toUrlValue() +"; zoom: "+this._map.getZoom() +"; size: "+this._map.getSize().toString() +"; c: "+this._map.getCenter().toUrlValue() ); |
---|
143 | GLog.writeUrl( url ); |
---|
144 | } |
---|
145 | |
---|
146 | this._previousLoadDataReq.box = box; |
---|
147 | this._previousLoadDataReq.zoom = this._map.getZoom(); |
---|
148 | this._previousLoadDataReq.resultBounds = null; |
---|
149 | this._dataLoading = true; |
---|
150 | |
---|
151 | try { |
---|
152 | GEvent.trigger( this, "dataloading" ); |
---|
153 | GDownloadUrl(url, this._onDataReceived.bind(this) ); |
---|
154 | } |
---|
155 | catch (e) { |
---|
156 | this._dataLoading = false; |
---|
157 | this._previousLoadDataReq.box=null; |
---|
158 | GEvent.trigger( this, "dataloadfailed", 600, e ); |
---|
159 | } |
---|
160 | }, |
---|
161 | |
---|
162 | _onDataReceived: function(data, responseCode) |
---|
163 | { |
---|
164 | var resp; |
---|
165 | try |
---|
166 | { |
---|
167 | if (responseCode!=200) |
---|
168 | { |
---|
169 | this._previousLoadDataReq.box=null; |
---|
170 | GEvent.trigger( this, "dataloadfailed", responseCode, null ); |
---|
171 | } |
---|
172 | else |
---|
173 | try |
---|
174 | { |
---|
175 | eval('resp = ' + data); |
---|
176 | if (resp.nb_items == undefined) |
---|
177 | throw new Error( "DATA DECODING ERROR" ); |
---|
178 | |
---|
179 | this._previousLoadDataReq.resultBounds = resp.bounds; |
---|
180 | if (document.is_debug && resp.debug) GLog.write( resp.debug ); |
---|
181 | GEvent.trigger( this, "dataloaded", resp ); |
---|
182 | } |
---|
183 | catch (e) { |
---|
184 | this._previousLoadDataReq.box=null; |
---|
185 | GEvent.trigger( this, "dataloadfailed", responseCode, e ); |
---|
186 | var s = e.message; |
---|
187 | s += '\n' + data.substr(0,1000); |
---|
188 | alert( s ); |
---|
189 | } |
---|
190 | } |
---|
191 | finally { |
---|
192 | this._dataLoading = false; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | } |
---|