FixedZoomLevels.js 12 KB

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