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

Last change on this file since 8220 was 8220, checked in by rvelices, 13 years ago

rv_gmaps internal - prepare migration to api V3

  • Property svn:eol-style set to LF
File size: 5.6 KB
Line 
1function PwgDataLoader( map, opts )
2{
3        this._map = map;
4        this.options = jQuery.fn.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 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, "moveend", this, this._onMapMoveEnd );
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_onMapMoveEnd: 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.getSize().height;
81
82        var lonRange = bounds.toSpan().lng();
83        var lonPrec = ( lonRange>=0 ? lonRange : 360-lonRange )* this.options.rectangle_of_confusion.width / this._map.getSize().width;
84
85        if ( this._previousLoadDataReq.box!=null )
86        { // not the first time
87                if ( this._previousLoadDataReq.box.containsBounds( bounds ) )
88                {
89                        if ( this._previousLoadDataReq.resultBounds == null )
90                                return; // no images here
91                        if ( this._map.getZoom() <= this._previousLoadDataReq.zoom )
92                                return;
93                        else
94                        {
95                                if (
96                                        this._map.getZoom() == this._previousLoadDataReq.zoom + 1
97                                        && this._map.getZoom() < this._map.getBoundsZoomLevel(this._previousLoadDataReq.resultBounds)
98                                        )
99                                {
100                                        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));
101                                        return;
102                                }
103                        }
104                }
105                else
106                {
107                }
108        }
109
110        var nd=0, sd=0, ed=0, wd=0;
111        if ( !bounds.isFullLat() )
112        {
113                nd = latRange*12/100;
114                sd = latRange*4/100;
115        }
116        if ( !bounds.isFullLng() )
117        {
118                ed = lonRange*9/100;
119                wd = lonRange*7/100;
120        }
121        var digits = Math.max( getLatLonDigits(latRange,4,2), getLatLonDigits(lonRange,4,2) );
122        var box = new google.maps.LatLngBounds( bounds.getSouthWest(), bounds.getNorthEast() );
123        box.extend( new google.maps.LatLng( Math.roundN(bounds.getSouthWest().lat()-sd,digits), Math.roundN( bounds.getSouthWest().lng()-wd,digits ) ) );
124        box.extend( new google.maps.LatLng( Math.roundN(bounds.getNorthEast().lat()+nd,digits), Math.roundN( bounds.getNorthEast().lng()+ed,digits) ) );
125
126        var url = this._urlMapData;
127        url += "&box=" + box.getSouthWest().toUrlValue(5) + "," + box.getNorthEast().toUrlValue(5);
128        url += "&lap=" +  Math.roundN( latPrec, getLatLonDigits(latPrec,1,1) ).toString();
129        url += "&lop=" +  Math.roundN( lonPrec, getLatLonDigits(lonPrec,1,1) ).toString();
130
131        if (document.is_debug) {
132                google.maps.Log.write("sd="+sd+" wd="+wd+" nd="+nd+" ed="+ed);
133                google.maps.Log.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() );
134                google.maps.Log.writeUrl( 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.replace('.php','x.php'),
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) google.maps.Log.write( 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.