]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/ArgParser.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Control / ArgParser.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5
6 /**
7  * @requires OpenLayers/Control.js
8  */
9
10 /**
11  * Class: OpenLayers.Control.ArgParser
12  * The ArgParser control adds location bar querystring parsing functionality 
13  * to an OpenLayers Map.
14  * When added to a Map control, on a page load/refresh, the Map will 
15  * automatically take the href string and parse it for lon, lat, zoom, and 
16  * layers information. 
17  *
18  * Inherits from:
19  *  - <OpenLayers.Control>
20  */
21 OpenLayers.Control.ArgParser = OpenLayers.Class(OpenLayers.Control, {
22
23     /**
24      * Parameter: center
25      * {<OpenLayers.LonLat>}
26      */
27     center: null,
28     
29     /**
30      * Parameter: zoom
31      * {int}
32      */
33     zoom: null,
34
35     /**
36      * Parameter: layers 
37      * {Array(<OpenLayers.Layer>)}
38      */
39     layers: null,
40     
41     /** 
42      * APIProperty: displayProjection
43      * {<OpenLayers.Projection>} Requires proj4js support. 
44      *     Projection used when reading the coordinates from the URL. This will
45      *
46      *     reproject the map coordinates from the URL into the map's
47      *     projection.
48      *
49      *     If you are using this functionality, be aware that any permalink
50      *     which is added to the map will determine the coordinate type which
51      *     is read from the URL, which means you should not add permalinks with
52      *     different displayProjections to the same map. 
53      */
54     displayProjection: null, 
55
56     /**
57      * Constructor: OpenLayers.Control.ArgParser
58      *
59      * Parameters:
60      * options - {Object}
61      */
62     initialize: function(options) {
63         OpenLayers.Control.prototype.initialize.apply(this, arguments);
64     },
65
66     /**
67      * Method: setMap
68      * Set the map property for the control. 
69      * 
70      * Parameters:
71      * map - {<OpenLayers.Map>} 
72      */
73     setMap: function(map) {
74         OpenLayers.Control.prototype.setMap.apply(this, arguments);
75
76         //make sure we dont already have an arg parser attached
77         for(var i=0, len=this.map.controls.length; i<len; i++) {
78             var control = this.map.controls[i];
79             if ( (control != this) &&
80                  (control.CLASS_NAME == "OpenLayers.Control.ArgParser") ) {
81                 
82                 // If a second argparser is added to the map, then we 
83                 // override the displayProjection to be the one added to the
84                 // map. 
85                 if (control.displayProjection != this.displayProjection) {
86                     this.displayProjection = control.displayProjection;
87                 }    
88                 
89                 break;
90             }
91         }
92         if (i == this.map.controls.length) {
93
94             var args = OpenLayers.Util.getParameters();
95             // Be careful to set layer first, to not trigger unnecessary layer loads
96             if (args.layers) {
97                 this.layers = args.layers;
98     
99                 // when we add a new layer, set its visibility 
100                 this.map.events.register('addlayer', this, 
101                                          this.configureLayers);
102                 this.configureLayers();
103             }
104             if (args.lat && args.lon) {
105                 this.center = new OpenLayers.LonLat(parseFloat(args.lon),
106                                                     parseFloat(args.lat));
107                 if (args.zoom) {
108                     this.zoom = parseInt(args.zoom);
109                 }
110     
111                 // when we add a new baselayer to see when we can set the center
112                 this.map.events.register('changebaselayer', this, 
113                                          this.setCenter);
114                 this.setCenter();
115             }
116         }
117     },
118    
119     /** 
120      * Method: setCenter
121      * As soon as a baseLayer has been loaded, we center and zoom
122      *   ...and remove the handler.
123      */
124     setCenter: function() {
125         
126         if (this.map.baseLayer) {
127             //dont need to listen for this one anymore
128             this.map.events.unregister('changebaselayer', this, 
129                                        this.setCenter);
130             
131             if (this.displayProjection) {
132                 this.center.transform(this.displayProjection, 
133                                       this.map.getProjectionObject()); 
134             }      
135
136             this.map.setCenter(this.center, this.zoom);
137         }
138     },
139
140     /** 
141      * Method: configureLayers
142      * As soon as all the layers are loaded, cycle through them and 
143      *   hide or show them. 
144      */
145     configureLayers: function() {
146
147         if (this.layers.length == this.map.layers.length) { 
148             this.map.events.unregister('addlayer', this, this.configureLayers);
149
150             for(var i=0, len=this.layers.length; i<len; i++) {
151                 
152                 var layer = this.map.layers[i];
153                 var c = this.layers.charAt(i);
154                 
155                 if (c == "B") {
156                     this.map.setBaseLayer(layer);
157                 } else if ( (c == "T") || (c == "F") ) {
158                     layer.setVisibility(c == "T");
159                 }
160             }
161         }
162     },     
163
164     CLASS_NAME: "OpenLayers.Control.ArgParser"
165 });