FixedZoomLevels.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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 = ['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 size = this.map.getSize();
  196. var tl = this.getLonLatFromViewPortPx({
  197. x: 0, y: 0
  198. });
  199. var br = this.getLonLatFromViewPortPx({
  200. x: size.w, y: size.h
  201. });
  202. if ((tl != null) && (br != null)) {
  203. return new OpenLayers.Bounds(tl.lon, br.lat, br.lon, tl.lat);
  204. } else {
  205. return null;
  206. }
  207. },
  208. /**
  209. * Method: getZoomForResolution
  210. * Get the zoom level for a given resolution
  211. *
  212. * Parameters:
  213. * resolution - {Float}
  214. *
  215. * Returns:
  216. * {Integer} A suitable zoom level for the specified resolution.
  217. * If no baselayer is set, returns null.
  218. */
  219. getZoomForResolution: function(resolution) {
  220. if (this.resolutions != null) {
  221. return OpenLayers.Layer.prototype.getZoomForResolution.apply(this, arguments);
  222. } else {
  223. var extent = OpenLayers.Layer.prototype.getExtent.apply(this, []);
  224. return this.getZoomForExtent(extent);
  225. }
  226. },
  227. /********************************************************/
  228. /* */
  229. /* Translation Functions */
  230. /* */
  231. /* The following functions translate GMaps and OL */
  232. /* formats for Pixel, LonLat, Bounds, and Zoom */
  233. /* */
  234. /********************************************************/
  235. //
  236. // TRANSLATION: MapObject Zoom <-> OpenLayers Zoom
  237. //
  238. /**
  239. * Method: getOLZoomFromMapObjectZoom
  240. * Get the OL zoom index from the map object zoom level
  241. *
  242. * Parameters:
  243. * moZoom - {Integer}
  244. *
  245. * Returns:
  246. * {Integer} An OpenLayers Zoom level, translated from the passed in zoom
  247. * Returns null if null value is passed in
  248. */
  249. getOLZoomFromMapObjectZoom: function(moZoom) {
  250. var zoom = null;
  251. if (moZoom != null) {
  252. zoom = moZoom - this.minZoomLevel;
  253. if (this.map.baseLayer !== this) {
  254. zoom = this.map.baseLayer.getZoomForResolution(
  255. this.getResolutionForZoom(zoom)
  256. );
  257. }
  258. }
  259. return zoom;
  260. },
  261. /**
  262. * Method: getMapObjectZoomFromOLZoom
  263. * Get the map object zoom level from the OL zoom level
  264. *
  265. * Parameters:
  266. * olZoom - {Integer}
  267. *
  268. * Returns:
  269. * {Integer} A MapObject level, translated from the passed in olZoom
  270. * Returns null if null value is passed in
  271. */
  272. getMapObjectZoomFromOLZoom: function(olZoom) {
  273. var zoom = null;
  274. if (olZoom != null) {
  275. zoom = olZoom + this.minZoomLevel;
  276. if (this.map.baseLayer !== this) {
  277. zoom = this.getZoomForResolution(
  278. this.map.baseLayer.getResolutionForZoom(zoom)
  279. );
  280. }
  281. }
  282. return zoom;
  283. },
  284. CLASS_NAME: "OpenLayers.Layer.FixedZoomLevels"
  285. });