Layer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. define("dojox/geo/openlayers/Layer", ["dojo/_base/kernel", "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/sniff"],
  2. function(dojo, declare, lang, array, sniff){
  3. return declare("dojox.geo.openlayers.Layer", null, {
  4. // summary:
  5. // Base layer class for dojox.geo.openlayers.Map specific layers extending OpenLayers.Layer class.
  6. // This layer class accepts Features which encapsulates graphic objects to be added to the map.
  7. // This layer class encapsulates an OpenLayers.Layer.
  8. // This class provides Feature management such as add, remove and feature access.
  9. constructor : function(name, options){
  10. // summary:
  11. // Constructs a new Layer.
  12. // name: String
  13. // The name of the layer.
  14. // options: Object
  15. // Options passed to the underlying OpenLayers.Layer object.
  16. var ol = options ? options.olLayer : null;
  17. if (!ol)
  18. ol = lang.delegate(new OpenLayers.Layer(name, options));
  19. this.olLayer = ol;
  20. this._features = null;
  21. this.olLayer.events.register("moveend", this, lang.hitch(this, this.moveTo));
  22. },
  23. renderFeature : function(/* Feature */f){
  24. // summary:
  25. // Called when rendering a feature is necessary.
  26. // f : Feature
  27. // The feature to draw.
  28. f.render();
  29. },
  30. getDojoMap : function(){
  31. return this.dojoMap;
  32. },
  33. addFeature : function(/* Feature | Array */f){
  34. // summary:
  35. // Add a feature or an array of features to the layer.
  36. // f : Feature or Array
  37. // The Feature or array of features to add.
  38. if (lang.isArray(f)) {
  39. array.forEach(f, function(item){
  40. this.addFeature(item);
  41. }, this);
  42. return;
  43. }
  44. if (this._features == null)
  45. this._features = [];
  46. this._features.push(f);
  47. f._setLayer(this);
  48. },
  49. removeFeature : function(/* Feature | Array */f){
  50. // summary :
  51. // Removes a feature or an array of features from the layer.
  52. // f : Feature or Array
  53. // The Feature or array of features to remove.
  54. var ft = this._features;
  55. if (ft == null)
  56. return;
  57. if (f instanceof Array) {
  58. f = f.slice(0);
  59. array.forEach(f, function(item){
  60. this.removeFeature(item);
  61. }, this);
  62. return;
  63. }
  64. var i = array.indexOf(ft, f);
  65. if (i != -1)
  66. ft.splice(i, 1);
  67. f._setLayer(null);
  68. f.remove();
  69. },
  70. removeFeatureAt : function(index){
  71. // summary:
  72. // Remove the feature at the specified index.
  73. // description:
  74. // Remove the feature at the specified index.
  75. // index: Number
  76. // The index of the feature to remove.
  77. var ft = this._features;
  78. var f = ft[index];
  79. if (!f)
  80. return;
  81. ft.splice(index, 1);
  82. f._setLayer(null);
  83. f.remove();
  84. },
  85. getFeatures : function(){
  86. // summary:
  87. // Retrieves the feature hold by this layer.
  88. // returns: Array
  89. // The untouched array of features hold by this layer.
  90. return this._features;
  91. },
  92. getFeatureAt : function(i){
  93. // summary:
  94. // Returns the i-th feature of this layer.
  95. // i : int
  96. // The index of the feature to return.
  97. // returns : ibm_maps.maps.Layer
  98. // The i-th feature of this layer.
  99. if (this._features == null)
  100. return undefined;
  101. return this._features[i];
  102. },
  103. getFeatureCount : function(){
  104. // summary:
  105. // Returns the number of the features contained by this layer.
  106. // returns: int
  107. // The number of the features contained by this layer.
  108. if (this._features == null)
  109. return 0;
  110. return this._features.length;
  111. },
  112. clear : function(){
  113. // summary:
  114. // Removes all the features from this layer.
  115. var fa = this.getFeatures();
  116. this.removeFeature(fa);
  117. },
  118. moveTo : function(event){
  119. // summary:
  120. // Called when the layer is panned or zoomed.
  121. // event: Object
  122. // The event
  123. if (event.zoomChanged) {
  124. if (this._features == null)
  125. return;
  126. array.forEach(this._features, function(f){
  127. this.renderFeature(f);
  128. }, this);
  129. }
  130. },
  131. redraw : function(){
  132. // summary:
  133. // Redraws this layer
  134. if (sniff.isIE)
  135. setTimeout(lang.hitch(this, function(){
  136. this.olLayer.redraw();
  137. }, 0));
  138. else
  139. this.olLayer.redraw();
  140. },
  141. added : function(){
  142. // summary:
  143. // Called when the layer is added to the map
  144. }
  145. });
  146. });