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

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

rv_gmaps towards full maps api v3 migration

  • Property svn:eol-style set to LF
File size: 5.0 KB
Line 
1function 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(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                }
95        }
96
97        var nd=0, sd=0, ed=0, wd=0;
98        /*if ( !bounds.isFullLat() )*/
99        {
100                nd = latRange*12/100;
101                sd = latRange*4/100;
102        }
103        /*if ( !bounds.isFullLng() )*/
104        {
105                ed = lonRange*9/100;
106                wd = lonRange*7/100;
107        }
108        var digits = Math.max( getLatLonDigits(latRange,4,2), getLatLonDigits(lonRange,4,2) );
109        var box = new google.maps.LatLngBounds( bounds.getSouthWest(), bounds.getNorthEast() );
110        box.extend( new google.maps.LatLng( Math.roundN(bounds.getSouthWest().lat()-sd,digits), Math.roundN( bounds.getSouthWest().lng()-wd,digits ) ) );
111        box.extend( new google.maps.LatLng( Math.roundN(bounds.getNorthEast().lat()+nd,digits), Math.roundN( bounds.getNorthEast().lng()+ed,digits) ) );
112
113        var url = this._urlMapData;
114        url += "&box=" + box.getSouthWest().toUrlValue(5) + "," + box.getNorthEast().toUrlValue(5);
115        url += "&lap=" +  Math.roundN( latPrec, getLatLonDigits(latPrec,1,1) ).toString();
116        url += "&lop=" +  Math.roundN( lonPrec, getLatLonDigits(lonPrec,1,1) ).toString();
117
118        if (document.is_debug) {
119                glog("sd="+sd+" wd="+wd+" nd="+nd+" ed="+ed);
120                glog( url );
121        }
122
123        this._previousLoadDataReq.box = box;
124        this._previousLoadDataReq.zoom = this._map.getZoom();
125        this._previousLoadDataReq.resultBounds = null;
126        this._dataLoading = true;
127
128        try {
129                google.maps.event.trigger( this, "dataloading" );
130                jQuery.ajax( {
131                        url: url,
132                        success: pwgBind(this, this._onAjaxSuccess),
133                        error: pwgBind(this, this._onAjaxError),
134                        });
135        }
136        catch (e) {
137                this._dataLoading = false;
138                this._previousLoadDataReq.box=null;
139                google.maps.event.trigger( this, "dataloadfailed", 600, e );
140        }
141},
142
143_onAjaxSuccess: function(data, textStatus, xhr)
144{
145        var resp;
146        try
147        {
148                eval('resp = ' + data);
149                if (resp.nb_items == undefined)
150                        throw new Error( "DATA DECODING ERROR" );
151                this._previousLoadDataReq.resultBounds = resp.bounds;
152                if (document.is_debug && resp.debug) glog( resp.debug );
153                google.maps.event.trigger( this, "dataloaded", resp );
154        }
155        catch (e)       {
156                this._previousLoadDataReq.box=null;
157                google.maps.event.trigger( this, "dataloadfailed", responseCode, e );
158                var s = e.message;
159                s += '\n' + data.substr(0,1000);
160                alert( s );
161        }
162        finally {
163                this._dataLoading = false;
164        }
165},
166
167_onAjaxError: function(xhr, textStatus, exc)
168{
169        try {
170                google.maps.event.trigger( this, "dataloadfailed", textStatus + xhr.status, exc );
171        }
172        catch (e) {
173        }
174        finally {
175                this._dataLoading = false;
176        }
177},
178
179}
Note: See TracBrowser for help on using the repository browser.