FixedZoomLevels.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/Layer.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.FixedZoomLevels
  10. * Some Layers will already have established zoom levels (like google
  11. * or ve). Instead of trying to determine them and populate a resolutions[]
  12. * Array with those values, we will hijack the resolution functionality
  13. * here.
  14. *
  15. * When you subclass FixedZoomLevels:
  16. *
  17. * The initResolutions() call gets nullified, meaning no resolutions[] array
  18. * is set up. Which would be a big problem getResolution() in Layer, since
  19. * it merely takes map.zoom and indexes into resolutions[]... but....
  20. *
  21. * The getResolution() call is also overridden. Instead of using the
  22. * resolutions[] array, we simply calculate the current resolution based
  23. * on the current extent and the current map size. But how will we be able
  24. * to calculate the current extent without knowing the resolution...?
  25. *
  26. * The getExtent() function is also overridden. Instead of calculating extent
  27. * based on the center point and the current resolution, we instead
  28. * calculate the extent by getting the lonlats at the top-left and
  29. * bottom-right by using the getLonLatFromViewPortPx() translation function,
  30. * taken from the pixel locations (0,0) and the size of the map. But how
  31. * will we be able to do lonlat-px translation without resolution....?
  32. *
  33. * The getZoomForResolution() method is overridden. Instead of indexing into
  34. * the resolutions[] array, we call OpenLayers.Layer.getExent(), passing in
  35. * the desired resolution. With this extent, we then call getZoomForExtent()
  36. *
  37. *
  38. * Whenever you implement a layer using OpenLayers.Layer.FixedZoomLevels,
  39. * it is your responsibility to provide the following three functions:
  40. *
  41. * - getLonLatFromViewPortPx
  42. * - getViewPortPxFromLonLat
  43. * - getZoomForExtent
  44. *
  45. * ...those three functions should generally be provided by any reasonable
  46. * API that you might be working from.
  47. *
  48. */
  49. OpenLayers.Layer.FixedZoomLevels = OpenLayers.Class({
  50. /********************************************************/
  51. /* */
  52. /* Baselayer Functions */
  53. /* */
  54. /* The following functions must all be implemented */
  55. /* by all base layers */
  56. /* */
  57. /********************************************************/
  58. /**
  59. * Constructor: OpenLayers.Layer.FixedZoomLevels
  60. * Create a new fixed zoom levels layer.
  61. */
  62. initialize: function() {
  63. //this class is only just to add the following functions...
  64. // nothing to actually do here... but it is probably a good
  65. // idea to have layers that use these functions call this
  66. // inititalize() anyways, in case at some point we decide we
  67. // do want to put some functionality or state in here.
  68. },
  69. /**
  70. * Method: initResolutions
  71. * Populate the resolutions array
  72. */
  73. initResolutions: function() {
  74. var props = new Array('minZoomLevel', 'maxZoomLevel', 'numZoomLevels');
  75. for(var i=0, len=props.length; i<len; i++) {
  76. var property = props[i];
  77. this[property] = (this.options[property] != null)
  78. ? this.options[property]
  79. : this.map[property];
  80. }
  81. if ( (this.minZoomLevel == null) ||
  82. (this.minZoomLevel < this.MIN_ZOOM_LEVEL) ){
  83. this.minZoomLevel = this.MIN_ZOOM_LEVEL;
  84. }
  85. //
  86. // At this point, we know what the minimum desired zoom level is, and
  87. // we must calculate the total number of zoom levels.
  88. //
  89. // Because we allow for the setting of either the 'numZoomLevels'
  90. // or the 'maxZoomLevel' properties... on either the layer or the
  91. // map, we have to define some rules to see which we take into
  92. // account first in this calculation.
  93. //
  94. // The following is the precedence list for these properties:
  95. //
  96. // (1) numZoomLevels set on layer
  97. // (2) maxZoomLevel set on layer
  98. // (3) numZoomLevels set on map
  99. // (4) maxZoomLevel set on map*
  100. // (5) none of the above*
  101. //
  102. // *Note that options (4) and (5) are only possible if the user
  103. // _explicitly_ sets the 'numZoomLevels' property on the map to
  104. // null, since it is set by default to 16.
  105. //
  106. //
  107. // Note to future: In 3.0, I think we should remove the default
  108. // value of 16 for map.numZoomLevels. Rather, I think that value
  109. // should be set as a default on the Layer.WMS class. If someone
  110. // creates a 3rd party layer and does not specify any 'minZoomLevel',
  111. // 'maxZoomLevel', or 'numZoomLevels', and has not explicitly
  112. // specified any of those on the map object either.. then I think
  113. // it is fair to say that s/he wants all the zoom levels available.
  114. //
  115. // By making map.numZoomLevels *null* by default, that will be the
  116. // case. As it is, I don't feel comfortable changing that right now
  117. // as it would be a glaring API change and actually would probably
  118. // break many peoples' codes.
  119. //
  120. //the number of zoom levels we'd like to have.
  121. var desiredZoomLevels;
  122. //this is the maximum number of zoom levels the layer will allow,
  123. // given the specified starting minimum zoom level.
  124. var limitZoomLevels = this.MAX_ZOOM_LEVEL - this.minZoomLevel + 1;
  125. if ( ((this.options.numZoomLevels == null) &&
  126. (this.options.maxZoomLevel != null)) // (2)
  127. ||
  128. ((this.numZoomLevels == null) &&
  129. (this.maxZoomLevel != null)) // (4)
  130. ) {
  131. //calculate based on specified maxZoomLevel (on layer or map)
  132. desiredZoomLevels = this.maxZoomLevel - this.minZoomLevel + 1;
  133. } else {
  134. //calculate based on specified numZoomLevels (on layer or map)
  135. // this covers cases (1) and (3)
  136. desiredZoomLevels = this.numZoomLevels;
  137. }
  138. if (desiredZoomLevels != null) {
  139. //Now that we know what we would *like* the number of zoom levels
  140. // to be, based on layer or map options, we have to make sure that
  141. // it does not conflict with the actual limit, as specified by
  142. // the constants on the layer itself (and calculated into the
  143. // 'limitZoomLevels' variable).
  144. this.numZoomLevels = Math.min(desiredZoomLevels, limitZoomLevels);
  145. } else {
  146. // case (5) -- neither 'numZoomLevels' not 'maxZoomLevel' was
  147. // set on either the layer or the map. So we just use the
  148. // maximum limit as calculated by the layer's constants.
  149. this.numZoomLevels = limitZoomLevels;
  150. }
  151. //now that the 'numZoomLevels' is appropriately, safely set,
  152. // we go back and re-calculate the 'maxZoomLevel'.
  153. this.maxZoomLevel = this.minZoomLevel + this.numZoomLevels - 1;
  154. if (this.RESOLUTIONS != null) {
  155. var resolutionsIndex = 0;
  156. this.resolutions = [];
  157. for(var i= this.minZoomLevel; i <= this.maxZoomLevel; i++) {
  158. this.resolutions[resolutionsIndex++] = this.RESOLUTIONS[i];
  159. }
  160. this.maxResolution = this.resolutions[0];
  161. this.minResolution = this.resolutions[this.resolutions.length - 1];
  162. }
  163. },
  164. /**
  165. * APIMethod: getResolution
  166. * Get the current map resolution
  167. *
  168. * Returns:
  169. * {Float} Map units per Pixel
  170. */
  171. getResolution: function() {
  172. if (this.resolutions != null) {
  173. return OpenLayers.Layer.prototype.getResolution.apply(this, arguments);
  174. } else {
  175. var resolution = null;
  176. var viewSize = this.map.getSize();
  177. var extent = this.getExtent();
  178. if ((viewSize != null) && (extent != null)) {
  179. resolution = Math.max( extent.getWidth() / viewSize.w,
  180. extent.getHeight() / viewSize.h );
  181. }
  182. return resolution;
  183. }
  184. },
  185. /**
  186. * APIMethod: getExtent
  187. * Calculates using px-> lonlat translation functions on tl and br
  188. * corners of viewport
  189. *
  190. * Returns:
  191. * {<OpenLayers.Bounds>} A Bounds object which represents the lon/lat
  192. * bounds of the current viewPort.
  193. */
  194. getExtent: function () {
  195. var extent = null;
  196. var size = this.map.getSize();
  197. var tlPx = new OpenLayers.Pixel(0,0);
  198. var tlLL = this.getLonLatFromViewPortPx(tlPx);
  199. var brPx = new OpenLayers.Pixel(size.w, size.h);
  200. var brLL = this.getLonLatFromViewPortPx(brPx);
  201. if ((tlLL != null) && (brLL != null)) {
  202. extent = new OpenLayers.Bounds(tlLL.lon,
  203. brLL.lat,
  204. brLL.lon,
  205. tlLL.lat);
  206. }
  207. return extent;
  208. },
  209. /**
  210. * Method: getZoomForResolution
  211. * Get the zoom level for a given resolution
  212. *
  213. * Parameters:
  214. * resolution - {Float}
  215. *
  216. * Returns:
  217. * {Integer} A suitable zoom level for the specified resolution.
  218. * If no baselayer is set, returns null.
  219. */
  220. getZoomForResolution: function(resolution) {
  221. if (this.resolutions != null) {
  222. return OpenLayers.Layer.prototype.getZoomForResolution.apply(this, arguments);
  223. } else {
  224. var extent = OpenLayers.Layer.prototype.getExtent.apply(this, []);
  225. return this.getZoomForExtent(extent);
  226. }
  227. },
  228. /********************************************************/
  229. /* */
  230. /* Translation Functions */
  231. /* */
  232. /* The following functions translate GMaps and OL */
  233. /* formats for Pixel, LonLat, Bounds, and Zoom */
  234. /* */
  235. /********************************************************/
  236. //
  237. // TRANSLATION: MapObject Zoom <-> OpenLayers Zoom
  238. //
  239. /**
  240. * Method: getOLZoomFromMapObjectZoom
  241. * Get the OL zoom index from the map object zoom level
  242. *
  243. * Parameters:
  244. * moZoom - {Integer}
  245. *
  246. * Returns:
  247. * {Integer} An OpenLayers Zoom level, translated from the passed in zoom
  248. * Returns null if null value is passed in
  249. */
  250. getOLZoomFromMapObjectZoom: function(moZoom) {
  251. var zoom = null;
  252. if (moZoom != null) {
  253. zoom = moZoom - this.minZoomLevel;
  254. if (this.map.baseLayer !== this) {
  255. zoom = this.map.baseLayer.getZoomForResolution(
  256. this.getResolutionForZoom(zoom)
  257. )
  258. }
  259. }
  260. return zoom;
  261. },
  262. /**
  263. * Method: getMapObjectZoomFromOLZoom
  264. * Get the map object zoom level from the OL zoom level
  265. *
  266. * Parameters:
  267. * olZoom - {Integer}
  268. *
  269. * Returns:
  270. * {Integer} A MapObject level, translated from the passed in olZoom
  271. * Returns null if null value is passed in
  272. */
  273. getMapObjectZoomFromOLZoom: function(olZoom) {
  274. var zoom = null;
  275. if (olZoom != null) {
  276. zoom = olZoom + this.minZoomLevel;
  277. if (this.map.baseLayer !== this) {
  278. zoom = this.getZoomForResolution(
  279. this.map.baseLayer.getResolutionForZoom(zoom)
  280. );
  281. }
  282. }
  283. return zoom;
  284. },
  285. CLASS_NAME: "OpenLayers.Layer.FixedZoomLevels"
  286. });