Scale.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @requires OpenLayers/Lang.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.Scale
  11. * The Scale control displays the current map scale as a ratio (e.g. Scale =
  12. * 1:1M). By default it is displayed in the lower right corner of the map.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Control>
  16. */
  17. OpenLayers.Control.Scale = OpenLayers.Class(OpenLayers.Control, {
  18. /**
  19. * Parameter: element
  20. * {DOMElement}
  21. */
  22. element: null,
  23. /**
  24. * APIProperty: geodesic
  25. * {Boolean} Use geodesic measurement. Default is false. The recommended
  26. * setting for maps in EPSG:4326 is false, and true EPSG:900913. If set to
  27. * true, the scale will be calculated based on the horizontal size of the
  28. * pixel in the center of the map viewport.
  29. */
  30. geodesic: false,
  31. /**
  32. * Constructor: OpenLayers.Control.Scale
  33. *
  34. * Parameters:
  35. * element - {DOMElement}
  36. * options - {Object}
  37. */
  38. initialize: function(element, options) {
  39. OpenLayers.Control.prototype.initialize.apply(this, [options]);
  40. this.element = OpenLayers.Util.getElement(element);
  41. },
  42. /**
  43. * Method: draw
  44. *
  45. * Returns:
  46. * {DOMElement}
  47. */
  48. draw: function() {
  49. OpenLayers.Control.prototype.draw.apply(this, arguments);
  50. if (!this.element) {
  51. this.element = document.createElement("div");
  52. this.div.appendChild(this.element);
  53. }
  54. this.map.events.register( 'moveend', this, this.updateScale);
  55. this.updateScale();
  56. return this.div;
  57. },
  58. /**
  59. * Method: updateScale
  60. */
  61. updateScale: function() {
  62. var scale;
  63. if(this.geodesic === true) {
  64. var units = this.map.getUnits();
  65. if(!units) {
  66. return;
  67. }
  68. var inches = OpenLayers.INCHES_PER_UNIT;
  69. scale = (this.map.getGeodesicPixelSize().w || 0.000001) *
  70. inches["km"] * OpenLayers.DOTS_PER_INCH;
  71. } else {
  72. scale = this.map.getScale();
  73. }
  74. if (!scale) {
  75. return;
  76. }
  77. if (scale >= 9500 && scale <= 950000) {
  78. scale = Math.round(scale / 1000) + "K";
  79. } else if (scale >= 950000) {
  80. scale = Math.round(scale / 1000000) + "M";
  81. } else {
  82. scale = Math.round(scale);
  83. }
  84. this.element.innerHTML = OpenLayers.i18n("Scale = 1 : ${scaleDenom}", {'scaleDenom':scale});
  85. },
  86. CLASS_NAME: "OpenLayers.Control.Scale"
  87. });