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

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 205 lines · 161 code · 28 blank · 16 comment · 27 complexity · e6d3bd76287ba2d60bfbfe2fa630f4e7 MD5 · raw file

  1. /*
  2. * ArcGIS for Google Maps Flash API
  3. *
  4. * License http://www.apache.org/licenses/LICENSE-2.0
  5. */ /**
  6. * @author nianwei at gmail dot com
  7. */
  8. package com.google.maps.extras.arcgislink {
  9. import com.google.maps.*;
  10. import com.google.maps.interfaces.*;
  11. import com.google.maps.overlays.OverlayBase;
  12. import flash.display.*;
  13. import flash.events.*;
  14. import flash.geom.Point;
  15. import flash.net.URLRequest;
  16. //import mx.controls.Image;
  17. /**
  18. * This is the UI component of an dynamic map service, used as an IOverlay.
  19. * It is implemented as an Image component overlay on top of base maps.
  20. */
  21. public class ArcGISMapOverlay extends OverlayBase {
  22. private var minZoom_:Number;
  23. private var maxZoom_:Number;
  24. private var mapService_:MapService;
  25. private var exportParams_:ImageParameters;
  26. private var drawing_:Boolean;
  27. private var redraw_:Boolean;
  28. private var map_:IMap;
  29. private var fullBounds_:LatLngBounds;
  30. private var initialBounds_:LatLngBounds;
  31. private var bounds_:LatLngBounds;
  32. private var moveend_:Boolean;
  33. private var img_:Sprite;
  34. public function ArcGISMapOverlay(service:*, opt_overlayOpts:ArcGISMapOverlayOptions=null) {
  35. super();
  36. opt_overlayOpts=opt_overlayOpts || new ArcGISMapOverlayOptions({});
  37. this.mapService_=(service is MapService) ? service as MapService : new MapService(service);
  38. var me:ArcGISMapOverlay=this;
  39. if (opt_overlayOpts.name) {
  40. this.mapService_.name=opt_overlayOpts.name;
  41. }
  42. this.alpha=opt_overlayOpts.alpha || 1;
  43. this.minZoom_=opt_overlayOpts.minResolution;
  44. this.maxZoom_=opt_overlayOpts.maxResolution;
  45. if (this.mapService_.hasLoaded()) {
  46. this.init_(opt_overlayOpts);
  47. } else {
  48. this.mapService_.addEventListener(ServiceEvent.LOAD, function(event:Event):void {
  49. me.init_(opt_overlayOpts);
  50. });
  51. }
  52. this.addEventListener(MapEvent.OVERLAY_ADDED, onOverlayAdded);
  53. this.addEventListener(MapEvent.OVERLAY_REMOVED, onOverlayRemoved);
  54. this.addEventListener(MapEvent.MAPTYPE_CHANGED, refresh);
  55. this.addEventListener(MapZoomEvent.CONTINUOUS_ZOOM_START, function(evt:Event):void {
  56. if (me.img_ != null) {
  57. me.img_.visible=false;
  58. }
  59. });
  60. }
  61. private function init_(opt_overlayOpts:ArcGISMapOverlayOptions):void {
  62. // this.opacity_ = opt_overlayOpts.opacity || 1;
  63. this.exportParams_=opt_overlayOpts.imageParameters || new ImageParameters({});
  64. // if the map imaging is been generated on server.
  65. this.drawing_=false;
  66. // if a follow up refresh is needed, normally cause by map view change before
  67. // the previous map has finished drawing.
  68. this.redraw_=false;
  69. if (this.img_ != null) {
  70. this.refresh();
  71. }
  72. this.dispatchEvent(new ServiceEvent(ServiceEvent.LOAD, this));
  73. }
  74. public override function getDefaultPane(map:IMap):IPane {
  75. this.map_=map;
  76. return map.getPaneManager().getPaneById(PaneId.PANE_OVERLAYS);
  77. }
  78. public override function positionOverlay(zoomChanged:Boolean):void {
  79. if (this.bounds_ && this.img_ != null && this.pane != null) {
  80. var nw:flash.geom.Point=this.pane.fromLatLngToPaneCoords(this.bounds_.getNorthWest());
  81. var se:flash.geom.Point=this.pane.fromLatLngToPaneCoords(this.bounds_.getSouthEast());
  82. var s:Sprite=this.img_;
  83. s.x=nw.x;
  84. s.y=nw.y;
  85. s.width=se.x - nw.x;
  86. s.height=se.y - nw.y;
  87. }
  88. }
  89. private function onOverlayAdded(event:MapEvent):void {
  90. this.map_.addEventListener(MapMoveEvent.MOVE_END, onMapMoveEnd);
  91. this.img_=new Sprite();
  92. this.addChild(this.img_);
  93. if (this.mapService_.hasLoaded()) {
  94. this.refresh();
  95. }
  96. }
  97. private function onOverlayRemoved(event:MapEvent):void {
  98. this.removeChild(this.img_);
  99. this.map_.removeEventListener(MapMoveEvent.MOVE_END, refresh);
  100. }
  101. private function onMapMoveEnd(event:MapMoveEvent):void {
  102. this.refresh();
  103. }
  104. public function refresh():void {
  105. if (!this.mapService_.hasLoaded() || this.map_ === null) {
  106. return;
  107. }
  108. if (this.drawing_ === true) {
  109. this.redraw_=true;
  110. return;
  111. }
  112. var bnds:LatLngBounds=this.map_.getLatLngBounds();
  113. var prj:IProjection=this.map_.getCurrentMapType().getProjection();
  114. var sr:SpatialReference;
  115. if (prj is ArcGISTileConfig) {
  116. sr=(prj as ArcGISTileConfig).getSpatialReference();
  117. } else {
  118. sr=SpatialReferences.WEB_MERCATOR;
  119. }
  120. this.img_.graphics.clear();
  121. var me:ArcGISMapOverlay=this;
  122. var params:ImageParameters=this.exportParams_;
  123. params.width=this.map_.getSize().x;
  124. params.height=this.map_.getSize().y;
  125. params.bounds=bnds;
  126. params.imageSpatialReference=sr;
  127. this.drawing_=true;
  128. this.dispatchEvent(new ServiceEvent(ServiceEvent.EXPORTMAP_START));
  129. this.mapService_.exportMap(params, function(json:MapImage):void {
  130. me.dispatchEvent(new ServiceEvent(ServiceEvent.EXPORTMAP_COMPLETE));
  131. me.drawing_=false;
  132. if (me.redraw_ === true) {
  133. me.redraw_=false;
  134. me.refresh();
  135. return;
  136. }
  137. if (json.href) {
  138. me.bounds_=ArcGISUtil.fromEnvelopeToLatLngBounds(json.extent);
  139. var loader:Loader=new Loader();
  140. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void {
  141. if (me.img_.numChildren > 0) {
  142. me.img_.removeChildAt(0);
  143. }
  144. me.img_.addChild(loader);
  145. me.img_.visible=true;
  146. me.positionOverlay(false);
  147. me.dispatchEvent(new ServiceEvent(ServiceEvent.EXPORTMAP_LOAD));
  148. });
  149. var request:URLRequest=new URLRequest(json.href);
  150. loader.load(request);
  151. } else {
  152. me.dispatchEvent(new ServiceEvent(ServiceEvent.EXPORTMAP_LOAD));
  153. }
  154. });
  155. }
  156. public function getMapService():MapService {
  157. return this.mapService_;
  158. }
  159. public function get fullBounds():LatLngBounds {
  160. this.fullBounds_=this.fullBounds_ || ArcGISUtil.fromEnvelopeToLatLngBounds(this.mapService_.fullExtent);
  161. return this.fullBounds_;
  162. }
  163. public function get initialBounds():LatLngBounds {
  164. this.initialBounds_=this.initialBounds_ || ArcGISUtil.fromEnvelopeToLatLngBounds(this.mapService_.initialExtent);
  165. return this.initialBounds_;
  166. }
  167. public function hasLoaded():Boolean {
  168. return this.mapService_.hasLoaded();
  169. }
  170. public function show():void {
  171. this.visible=true;
  172. }
  173. public function hide():void {
  174. this.visible=false;
  175. }
  176. }
  177. }