/src/com/google/maps/extras/arcgislink/Layer.as

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 177 lines · 111 code · 15 blank · 51 comment · 20 complexity · a55b73c3b8baf9ae6ddfe1d1717f91d5 MD5 · raw file

  1. /*
  2. * ArcGIS for Google Maps Flash API
  3. *
  4. * License http://www.apache.org/licenses/LICENSE-2.0
  5. */
  6. /**
  7. * @author nianwei at gmail dot com
  8. */
  9. package com.google.maps.extras.arcgislink {
  10. import flash.events.*;
  11. /**
  12. * This class represents a Map layer inside an ArcGISMapService. It carries
  13. * information about a layer's name, id and other information such as scales etc.
  14. * Due to the way REST API is implemented, each layers extra meta data must retrieved
  15. * individually. However, most operations do not require those extra info and can be
  16. * used directly.
  17. */
  18. // dynamic for now
  19. public dynamic class Layer implements IEventDispatcher {
  20. public var url:String;
  21. private var loaded_:Boolean;
  22. private var correct_:Boolean;
  23. public var definition:String;
  24. public var id:int;
  25. public var name:String;
  26. public var visible:Boolean;
  27. /**
  28. * Create a map Layer using it's url ( http://[mapservice-url]/[layerId])
  29. * @name ArcGISLayer
  30. * @param {String} url
  31. * @property {Number} [id] layer ID
  32. * @property {String} [name] layer Name
  33. * @property {Number} [parentLayerId] parent LayerId
  34. * @property {Layer} [parentLayer] parent Layer {@link ArcGISLayer}
  35. * @property {Boolean} [defaultVisibility] defaultVisibility
  36. * @property {Number[]} [subLayerIds] sub LayerIds. null if no sub layers
  37. * @property {Layer[]} [subLayers] sub Layers. {@link ArcGISLayer}[].
  38. * @property {Boolean} [visibility] Visibility of this layer
  39. * @property {String} [definition] Layer definition.
  40. * @property {String} [type] layer type(Feature Layer|), only available after load.
  41. * @property {String} [geometryType] geometryType type(esriGeometryPoint|..), only available after load.
  42. * @property {String} [copyrightText] copyrightText, only available after load.
  43. * @property {Number} [minScale] minScale, only available after load.
  44. * @property {Number} [maxScale] maxScale, only available after load.
  45. * @property {Envelope} [extent] extent, only available after load.
  46. * @property {String} [displayField] displayField, only available after load.
  47. * @property {Field[]} [fields] fields, only available after load. See {@link ArcGISField}
  48. */
  49. public function Layer(url:String, opts:LayerOptions=null) {
  50. this.url=url;
  51. this.loaded_=false;
  52. this.correct_=false;
  53. this.definition=null;
  54. dispatcher_=new EventDispatcher(this);
  55. if (opts!=null && opts.initLoad===false){
  56. return ;
  57. }else{
  58. loadInfo();
  59. }
  60. }
  61. public function loadInfo(opt_callback:Function=null):void {
  62. trace('ArcGISLayer.loadInfo:'+this.url);
  63. var me:Layer=this;
  64. if (this.loaded_ && this.correct_) {
  65. return;
  66. }
  67. ArcGISUtil.restRequest(this.url, {f: 'json'}, this, function(json:Object):void {
  68. if (json.error) {
  69. me.correct_=false;
  70. } else {
  71. me.correct_=true;
  72. ArcGISUtil.augmentObject(json, me);
  73. }
  74. me.loaded_=true;
  75. me.dispatchEvent(new ServiceEvent(ServiceEvent.LOAD));
  76. if (opt_callback !== null) {
  77. opt_callback.call(null, me);
  78. }
  79. });
  80. }
  81. public function hasLoaded():Boolean {
  82. return this.loaded_;
  83. }
  84. public function getFieldNames():Array {
  85. var ret:Array=[];
  86. if (this.hasLoaded()) {
  87. for (var i:int=0; i < this.fields.length; i++) {
  88. ret.push(this.fields[i].name);
  89. }
  90. }
  91. return ret;
  92. }
  93. ;
  94. /**
  95. * Whether the layer is viewable at given scale
  96. * @param {Number} scale
  97. * @return {Boolean}
  98. */
  99. public function isInScale(scale:Number):Boolean {
  100. // note if the layer's extra info is not loaded, it will return true
  101. if (this.maxScale && this.maxScale > scale) {
  102. return false;
  103. }
  104. if (this.minScale && this.minScale < scale) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. ;
  110. /**
  111. * The query operation is performed on a layer resource. The result of this operation is a resultset resource that will be
  112. * passed in the callback function. param is an instance of {@link ArcGISQueryParameters}
  113. * <br/>For more info see <a href = 'http://resources.esri.com/help/9.3/arcgisserver/apis/rest/query.html'>Query Operation</a>.
  114. * @param {QueryParameters} params
  115. * @param {Function} callback
  116. */
  117. public function query(qparams:QueryParameters, callbackFn:Function=null, failedFn:Function=null, ovOpts:OverlayOptions=null):void {
  118. if (!qparams) {
  119. return;
  120. }
  121. var params:Object={
  122. f:'json',
  123. returnGeometry:qparams.returnGeometry === false ? false : true,
  124. where:qparams.where,
  125. outSR:SpatialReferences.WGS84.wkid
  126. };
  127. if (qparams.outFields && qparams.outFields.length>0){
  128. params.outFields=qparams.outFields.join(',');
  129. }
  130. if (qparams.geometry && qparams.geometry.length>0){
  131. params.geometry=ArcGISUtil.fromGeometryToJSON(ArcGISUtil.fromOverlaysToGeometry(qparams.geometry, SpatialReferences.WGS84));
  132. params.spatialRel=qparams.spatialRelationship;
  133. params.inSR=SpatialReferences.WGS84.wkid;
  134. }
  135. var me:Layer=this;
  136. ArcGISUtil.restRequest(this.url + '/query', params, this, function(json:*):void {
  137. var res:ResultSet=new ResultSet(json);
  138. if (callbackFn != null) {
  139. callbackFn.call(me, res);
  140. }
  141. me.dispatchEvent(new ServiceEvent(ServiceEvent.QUERY_COMPLETE, res));
  142. }, failedFn);
  143. }
  144. ;
  145. private var dispatcher_:EventDispatcher;
  146. public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
  147. dispatcher_.addEventListener(type, listener, useCapture, priority);
  148. }
  149. public function dispatchEvent(evt:Event):Boolean {
  150. return dispatcher_.dispatchEvent(evt);
  151. }
  152. public function hasEventListener(type:String):Boolean {
  153. return dispatcher_.hasEventListener(type);
  154. }
  155. public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
  156. dispatcher_.removeEventListener(type, listener, useCapture);
  157. }
  158. public function willTrigger(type:String):Boolean {
  159. return dispatcher_.willTrigger(type);
  160. }
  161. }
  162. }