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