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