ArgParser.js 5.7 KB

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