source: extensions/rv_gmaps/trunk/template/data_loader.js @ 12698

Last change on this file since 12698 was 12698, checked in by rvelices, 12 years ago

rv_gmaps towards full maps api v3 migration (still to test)

  • Property svn:eol-style set to LF
File size: 5.4 KB
Line 
1function PwgDataLoader( map, opts )
2{
3        this._map = map;
4        this.options = jQuery.fn.extend(
5                {
6                        reload_data_timeout: 500,
7                        rectangle_of_confusion: new google.maps.Size(32,16)
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)
17getLatLonDigits = 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
24Math.roundN = function(d, digits) { var c = Math.pow(10,digits); return Math.round(d*c)/c; }
25
26
27PwgDataLoader.prototype =  {
28
29
30_urlMapData: null,
31_timerReloadData: null,
32_dataLoading: false,
33
34_previousLoadDataReq : {box: null, zoom:0, resultBounds:null},
35
36start: 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
44terminate: function()
45{
46        this.clearTimerReloadData();
47        this._map = null;
48},
49
50clearTimerReloadData : 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 = this._map.getBounds();
78
79        var latRange = bounds.toSpan().lat();
80        var latPrec = latRange * this.options.rectangle_of_confusion.height / this._map.getDiv().offsetHeight;
81
82        var lonRange = bounds.toSpan().lng();
83        var lonPrec = ( lonRange>=0 ? lonRange : 360-lonRange )* this.options.rectangle_of_confusion.width / this._map.getDiv().offsetWidth;
84
85        if ( this._previousLoadDataReq.box!=null )
86        { // not the first time
87                if ( this._previousLoadDataReq.box.contains( bounds.getNorthEast() )
88                                        && this._previousLoadDataReq.box.contains( bounds.getSouthWest() ))
89                {
90                        if ( this._previousLoadDataReq.resultBounds == null )
91                                return; // no images here
92                        if ( this._map.getZoom() <= this._previousLoadDataReq.zoom )
93                                return;
94                        else
95                        {
96                                if (
97                                        this._map.getZoom() == this._previousLoadDataReq.zoom + 1
98                                        && this._map.getZoom() < this._map.getBoundsZoomLevel(this._previousLoadDataReq.resultBounds)
99                                        )
100                                {
101                                        if (document.is_debug) google.maps.Log.write('no load: zoom crt: '+this._map.getZoom()+'; prev: '+this._previousLoadDataReq.zoom+'; target: '+this._map.getBoundsZoomLevel(this._previousLoadDataReq.resultBounds));
102                                        return;
103                                }
104                        }
105                }
106                else
107                {
108                }
109        }
110
111        var nd=0, sd=0, ed=0, wd=0;
112        /*if ( !bounds.isFullLat() )*/
113        {
114                nd = latRange*12/100;
115                sd = latRange*4/100;
116        }
117        /*if ( !bounds.isFullLng() )*/
118        {
119                ed = lonRange*9/100;
120                wd = lonRange*7/100;
121        }
122        var digits = Math.max( getLatLonDigits(latRange,4,2), getLatLonDigits(lonRange,4,2) );
123        var box = new google.maps.LatLngBounds( bounds.getSouthWest(), bounds.getNorthEast() );
124        box.extend( new google.maps.LatLng( Math.roundN(bounds.getSouthWest().lat()-sd,digits), Math.roundN( bounds.getSouthWest().lng()-wd,digits ) ) );
125        box.extend( new google.maps.LatLng( Math.roundN(bounds.getNorthEast().lat()+nd,digits), Math.roundN( bounds.getNorthEast().lng()+ed,digits) ) );
126
127        var url = this._urlMapData;
128        url += "&box=" + box.getSouthWest().toUrlValue(5) + "," + box.getNorthEast().toUrlValue(5);
129        url += "&lap=" +  Math.roundN( latPrec, getLatLonDigits(latPrec,1,1) ).toString();
130        url += "&lop=" +  Math.roundN( lonPrec, getLatLonDigits(lonPrec,1,1) ).toString();
131
132        if (document.is_debug) {
133                glog("sd="+sd+" wd="+wd+" nd="+nd+" ed="+ed);
134                glog( url );
135        }
136
137        this._previousLoadDataReq.box = box;
138        this._previousLoadDataReq.zoom = this._map.getZoom();
139        this._previousLoadDataReq.resultBounds = null;
140        this._dataLoading = true;
141
142        try {
143                google.maps.event.trigger( this, "dataloading" );
144                jQuery.ajax( {
145                        url: url,
146                        success: pwgBind(this, this._onAjaxSuccess),
147                        error: pwgBind(this, this._onAjaxError),
148                        });
149        }
150        catch (e) {
151                this._dataLoading = false;
152                this._previousLoadDataReq.box=null;
153                google.maps.event.trigger( this, "dataloadfailed", 600, e );
154        }
155},
156
157_onAjaxSuccess: function(data, textStatus, xhr)
158{
159        var resp;
160        try
161        {
162                eval('resp = ' + data);
163                if (resp.nb_items == undefined)
164                        throw new Error( "DATA DECODING ERROR" );
165                this._previousLoadDataReq.resultBounds = resp.bounds;
166                if (document.is_debug && resp.debug) glog( resp.debug );
167                google.maps.event.trigger( this, "dataloaded", resp );
168        }
169        catch (e)       {
170                this._previousLoadDataReq.box=null;
171                google.maps.event.trigger( this, "dataloadfailed", responseCode, e );
172                var s = e.message;
173                s += '\n' + data.substr(0,1000);
174                alert( s );
175        }
176        finally {
177                this._dataLoading = false;
178        }
179},
180
181_onAjaxError: function(xhr, textStatus, exc)
182{
183        try {
184                google.maps.event.trigger( this, "dataloadfailed", textStatus + xhr.status, exc );
185        }
186        catch (e) {
187        }
188        finally {
189                this._dataLoading = false;
190        }
191},
192
193}
Note: See TracBrowser for help on using the repository browser.