1 | function PwgDataLoader( map, opts ) |
---|
2 | { |
---|
3 | this._map = map; |
---|
4 | this.options = jQuery.fn.extend( |
---|
5 | { |
---|
6 | reload_data_timeout: 200, |
---|
7 | rectangle_of_confusion: new google.maps.Size(16,32) |
---|
8 | } |
---|
9 | , opts || {} ); |
---|
10 | this.options.rectangle_of_confusion = new google.maps.Size( 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 | google.maps.event.bind( this._map, "movestart", this, this.clearTimerReloadData ); |
---|
40 | google.maps.event.bind( this._map, "idle", this, this._onIdle ); |
---|
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 | _onIdle: function() |
---|
62 | { |
---|
63 | this.clearTimerReloadData(); |
---|
64 | this._timerReloadData = setTimeout( pwgBind(this, this._onTimeoutLoadData), 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 = new google.maps.LatLngBounds( this._map.getBounds().getSouthWest(), this._map.getBounds().getNorthEast() ); |
---|
78 | |
---|
79 | //bug as of v 3.7 - |
---|
80 | var isOver = false; |
---|
81 | if (bounds.getSouthWest().lng() < bounds.getNorthEast().lng()) |
---|
82 | { |
---|
83 | if ( this._map.getCenter().lng()<bounds.getSouthWest().lng() || this._map.getCenter().lng()>bounds.getNorthEast().lng() ) |
---|
84 | { |
---|
85 | isOver = true; |
---|
86 | } |
---|
87 | } |
---|
88 | else if ( this._map.getCenter().lng()>bounds.getNorthEast().lng() && this._map.getCenter().lng()<bounds.getSouthWest().lng() ) |
---|
89 | { |
---|
90 | isOver = true; |
---|
91 | } |
---|
92 | |
---|
93 | if (isOver) |
---|
94 | bounds = new google.maps.LatLngBounds( new google.maps.LatLng(this._map.getBounds().getSouthWest().lat(),-179.9), new google.maps.LatLng(this._map.getBounds().getNorthEast().lat(), 179.9) ); |
---|
95 | //end bug |
---|
96 | |
---|
97 | |
---|
98 | var latRange = bounds.toSpan().lat(); |
---|
99 | var latPrec = latRange * this.options.rectangle_of_confusion.height / this._map.getDiv().offsetHeight; |
---|
100 | |
---|
101 | var lonRange = bounds.toSpan().lng(); |
---|
102 | var lonPrec = ( lonRange>=0 ? lonRange : 360-lonRange )* this.options.rectangle_of_confusion.width / this._map.getDiv().offsetWidth; |
---|
103 | |
---|
104 | if ( this._previousLoadDataReq.box!=null ) |
---|
105 | { // not the first time |
---|
106 | if ( this._previousLoadDataReq.box.contains( bounds.getNorthEast() ) |
---|
107 | && this._previousLoadDataReq.box.contains( bounds.getSouthWest() )) |
---|
108 | { |
---|
109 | if ( this._previousLoadDataReq.resultBounds == null ) |
---|
110 | return; // no images here |
---|
111 | if ( this._map.getZoom() <= this._previousLoadDataReq.zoom ) |
---|
112 | return; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | var nd=0, sd=0, ed=0, wd=0; |
---|
117 | /*if ( !bounds.isFullLat() )*/ |
---|
118 | { |
---|
119 | nd = latRange*12/100; |
---|
120 | sd = latRange*4/100; |
---|
121 | } |
---|
122 | /*if ( !bounds.isFullLng() )*/ |
---|
123 | { |
---|
124 | ed = lonRange*9/100; |
---|
125 | wd = lonRange*7/100; |
---|
126 | } |
---|
127 | var digits = Math.max( getLatLonDigits(latRange,4,2), getLatLonDigits(lonRange,4,2) ); |
---|
128 | var box = new google.maps.LatLngBounds( bounds.getSouthWest(), bounds.getNorthEast() ); |
---|
129 | box.extend( new google.maps.LatLng( Math.roundN(bounds.getSouthWest().lat()-sd,digits), Math.roundN( bounds.getSouthWest().lng()-wd,digits ) ) ); |
---|
130 | box.extend( new google.maps.LatLng( Math.roundN(bounds.getNorthEast().lat()+nd,digits), Math.roundN( bounds.getNorthEast().lng()+ed,digits) ) ); |
---|
131 | |
---|
132 | var url = this._urlMapData; |
---|
133 | url += "&box=" + box.getSouthWest().toUrlValue(5) + "," + box.getNorthEast().toUrlValue(5); |
---|
134 | url += "&lap=" + Math.roundN( latPrec, getLatLonDigits(latPrec,1,1) ).toString(); |
---|
135 | url += "&lop=" + Math.roundN( lonPrec, getLatLonDigits(lonPrec,1,1) ).toString(); |
---|
136 | |
---|
137 | if (document.is_debug) { |
---|
138 | glog("sd="+sd+" wd="+wd+" nd="+nd+" ed="+ed); |
---|
139 | glog( url ); |
---|
140 | } |
---|
141 | |
---|
142 | this._previousLoadDataReq.box = box; |
---|
143 | this._previousLoadDataReq.zoom = this._map.getZoom(); |
---|
144 | this._previousLoadDataReq.resultBounds = null; |
---|
145 | this._dataLoading = true; |
---|
146 | |
---|
147 | try { |
---|
148 | google.maps.event.trigger( this, "dataloading" ); |
---|
149 | jQuery.ajax( { |
---|
150 | url: url, |
---|
151 | success: pwgBind(this, this._onAjaxSuccess), |
---|
152 | error: pwgBind(this, this._onAjaxError), |
---|
153 | }); |
---|
154 | } |
---|
155 | catch (e) { |
---|
156 | this._dataLoading = false; |
---|
157 | this._previousLoadDataReq.box=null; |
---|
158 | google.maps.event.trigger( this, "dataloadfailed", 600, e ); |
---|
159 | } |
---|
160 | }, |
---|
161 | |
---|
162 | _onAjaxSuccess: function(data, textStatus, xhr) |
---|
163 | { |
---|
164 | var resp; |
---|
165 | try |
---|
166 | { |
---|
167 | eval('resp = ' + data); |
---|
168 | if (resp.nb_items == undefined) |
---|
169 | throw new Error( "DATA DECODING ERROR" ); |
---|
170 | this._previousLoadDataReq.resultBounds = resp.bounds; |
---|
171 | if (document.is_debug && resp.debug) glog( resp.debug ); |
---|
172 | google.maps.event.trigger( this, "dataloaded", resp ); |
---|
173 | } |
---|
174 | catch (e) { |
---|
175 | this._previousLoadDataReq.box=null; |
---|
176 | google.maps.event.trigger( this, "dataloadfailed", responseCode, e ); |
---|
177 | var s = e.message; |
---|
178 | s += '\n' + data.substr(0,1000); |
---|
179 | alert( s ); |
---|
180 | } |
---|
181 | finally { |
---|
182 | this._dataLoading = false; |
---|
183 | } |
---|
184 | }, |
---|
185 | |
---|
186 | _onAjaxError: function(xhr, textStatus, exc) |
---|
187 | { |
---|
188 | try { |
---|
189 | google.maps.event.trigger( this, "dataloadfailed", textStatus + xhr.status, exc ); |
---|
190 | } |
---|
191 | catch (e) { |
---|
192 | } |
---|
193 | finally { |
---|
194 | this._dataLoading = false; |
---|
195 | } |
---|
196 | }, |
---|
197 | |
---|
198 | } |
---|