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