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. */
7 * @requires OpenLayers/Control.js
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
19 * - <OpenLayers.Control>
21 OpenLayers.Control.ArgParser = OpenLayers.Class(OpenLayers.Control, {
25 * {<OpenLayers.LonLat>}
37 * {Array(<OpenLayers.Layer>)}
42 * APIProperty: displayProjection
43 * {<OpenLayers.Projection>} Requires proj4js support.
44 * Projection used when reading the coordinates from the URL. This will
46 * reproject the map coordinates from the URL into the map's
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.
54 displayProjection: null,
57 * Constructor: OpenLayers.Control.ArgParser
62 initialize: function(options) {
63 OpenLayers.Control.prototype.initialize.apply(this, arguments);
68 * Set the map property for the control.
71 * map - {<OpenLayers.Map>}
73 setMap: function(map) {
74 OpenLayers.Control.prototype.setMap.apply(this, arguments);
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") ) {
82 // If a second argparser is added to the map, then we
83 // override the displayProjection to be the one added to the
85 if (control.displayProjection != this.displayProjection) {
86 this.displayProjection = control.displayProjection;
92 if (i == this.map.controls.length) {
94 var args = OpenLayers.Util.getParameters();
95 // Be careful to set layer first, to not trigger unnecessary layer loads
97 this.layers = args.layers;
99 // when we add a new layer, set its visibility
100 this.map.events.register('addlayer', this,
101 this.configureLayers);
102 this.configureLayers();
104 if (args.lat && args.lon) {
105 this.center = new OpenLayers.LonLat(parseFloat(args.lon),
106 parseFloat(args.lat));
108 this.zoom = parseInt(args.zoom);
111 // when we add a new baselayer to see when we can set the center
112 this.map.events.register('changebaselayer', this,
121 * As soon as a baseLayer has been loaded, we center and zoom
122 * ...and remove the handler.
124 setCenter: function() {
126 if (this.map.baseLayer) {
127 //dont need to listen for this one anymore
128 this.map.events.unregister('changebaselayer', this,
131 if (this.displayProjection) {
132 this.center.transform(this.displayProjection,
133 this.map.getProjectionObject());
136 this.map.setCenter(this.center, this.zoom);
141 * Method: configureLayers
142 * As soon as all the layers are loaded, cycle through them and
145 configureLayers: function() {
147 if (this.layers.length == this.map.layers.length) {
148 this.map.events.unregister('addlayer', this, this.configureLayers);
150 for(var i=0, len=this.layers.length; i<len; i++) {
152 var layer = this.map.layers[i];
153 var c = this.layers.charAt(i);
156 this.map.setBaseLayer(layer);
157 } else if ( (c == "T") || (c == "F") ) {
158 layer.setVisibility(c == "T");
164 CLASS_NAME: "OpenLayers.Control.ArgParser"