TypeError: Error #1009: Cannot access a property or method of a null object reference. at com.esri.ags.layers::Layer/updateLayerIfInvalid()at com.esri.ags.layers::Layer/updateDisplayList() at mx.core::UIComponent/validateDisplayList() at mx.managers::LayoutManager/validateDisplayList() at mx.managers::LayoutManager/doPhasedInstantiation() at mx.managers::LayoutManager/doPhasedInstantiationCallback()
registerClassAlias("com.esri.ags.Graphic", Graphic); registerClassAlias("com.esri.ags.SpatialReference", SpatialReference); registerClassAlias("com.esri.ags.FeatureSet", FeatureSet); registerClassAlias("com.esri.ags.Map", Map); registerClassAlias("com.esri.ags.layers.ArcGISDynamicMapServiceLayer", ArcGISDynamicMapServiceLayer); registerClassAlias("com.esri.ags.layers.ArcGISTiledMapServiceLayer",ArcGISTiledMapServiceLayer); registerClassAlias("com.esri.ags.layers.GraphicsLayer", GraphicsLayer); registerClassAlias("com.esri.ags.layers.Layer", Layer); registerClassAlias("com.esri.ags.geometry.MapPoint", MapPoint); registerClassAlias("com.esri.ags.geometry.Multipoint", Multipoint); registerClassAlias("com.esri.ags.geometry.Polygon", Polygon); registerClassAlias("com.esri.ags.geometry.Polyline", Polyline); registerClassAlias("com.esri.ags.geometry.Extent", Extent); //Generic Symbols registerClassAlias("com.esri.ags.symbols.CompositeSymbol",CompositeSymbol); registerClassAlias("com.esri.ags.symbols.Symbol",Symbol); //Point Symbols registerClassAlias("com.esri.ags.symbols.SimpleMarkerSymbol",SimpleMarkerSymbol); registerClassAlias("com.esri.ags.symbols.PictureMarkerSymbol",PictureMarkerSymbol); registerClassAlias("com.esri.ags.symbols.TextSymbol",TextSymbol); registerClassAlias("com.esri.ags.symbols.InfoSymbol",InfoSymbol); //Line Symbols registerClassAlias("com.esri.ags.symbosl.LineSymbol", LineSymbol); registerClassAlias("com.esri.ags.symbols.SimpleLineSymbol", SimpleLineSymbol); registerClassAlias("com.esri.ags.symbols.CartographicLineSymbol", CartographicLineSymbol); //Polygon Symbols registerClassAlias("com.esri.ags.symbols.SimpleFillSymbol",SimpleFillSymbol); registerClassAlias("com.esri.ags.symbols.PictureFillSymbol", PictureFillSymbol); for each(var oLayer:Layer in map.layers) { if (oLayer is ArcGISDynamicMapServiceLayer) { var cLayer:ArcGISDynamicMapServiceLayer = deepClone(oLayer) as ArcGISDynamicMapServiceLayer; //cLayer.addEventListener(Event.COMPLETE,onLoadDone); printMap.addLayer(cLayer); } else if (oLayer is ArcGISTiledMapServiceLayer) { var tLayer:ArcGISTiledMapServiceLayer = deepClone(oLayer) as ArcGISTiledMapServiceLayer; /* var cLayer2:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(tLayer.url); cLayer2.id = tLayer.id; cLayer2.imageFormat = "jpg"; cLayer2.addEventListener(Event.COMPLETE,onLoadDone); printMap.addLayer(cLayer2); */ printMap.addLayer(tLayer); } else if (oLayer is GraphicsLayer) { var gLayer:GraphicsLayer = deepClone(oLayer) as GraphicsLayer; printMap.addLayer(gLayer); } } printMap.lods = null; printMap.scale = map.scale; printMap.scaleBarVisible = true; printMap.panArrowsVisible = false; printMap.zoomSliderVisible = false; printMap.logoVisible = false; printMap.useHandCursor = false; var mp:MapPoint=new MapPoint(map.extent.center.x, map.extent.center.y) //printMap.centerAt(mp); printMap.extent=map.extent; return componentPrintBorderContainer;
private function deepClone ( obj : * ) : * { var bytes : ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject(); }
So why doesn't the new 10.1 printing stuff solve this problem for you?
Your custom solution is very cool though, just no longer necessary....
Yes, the new 10.1 print workflow will do all of those things and more, except possibly the measurement tool part (I haven't tested that).
protected function button1_clickHandler(event:MouseEvent):void { var pdf:PDF = new PDF(Orientation.LANDSCAPE,Unit.INCHES,Size.LETTER); pdf.setDisplayMode (Display.FULL_PAGE); pdf.setMargins(0.5,0.5,0.5,0.5); pdf.addPage(); pdf.setAutoPageBreak(false,0.5); //Use the PDF page margins for the width and height of printable map area. //The content width and height are the desired map size on the page. This example is //just fitting the map to the margins. var marginsRectangle:Rectangle = pdf.getMargins(); var contentWidth:Number = marginsRectangle.width; var contentHeigth:Number = marginsRectangle.height ; var newBitmap:Bitmap = PrintMapUtil.trimmedMap(map, contentWidth, contentHeigth); // Add the map image pdf.addImage(newBitmap, null ,0, 0, contentWidth , contentHeigth , 0, 1, true, ImageFormat.JPG, 75); pdf.save(Method.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", Download.ATTACHMENT, "report.pdf"); }
public static function trimmedMap(map:Map, printAreaWidth:Number, printAreaHeight:Number):Bitmap { var rectangle:Rectangle = clipRectangle(map, printAreaWidth, printAreaHeight); //get the map as bitmapdata so the part we want can be copied and clipped. var myBitmapData:BitmapData = new BitmapData(map.width,map.height); myBitmapData.draw(map,null,null,null,rectangle,true); // create a new cropped map image to fit the print area. var newBitmapData:BitmapData = new BitmapData(rectangle.width, rectangle.height) newBitmapData.copyPixels(myBitmapData, rectangle, new Point(0, 0)); return new Bitmap(newBitmapData); }
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import com.esri.ags.layers.ArcGISTiledMapServiceLayer; import com.esri.ags.layers.Layer; import flash.net.registerClassAlias; protected function button1_clickHandler(event:MouseEvent):void { registerClassAlias("com.esri.ags.layers.ArcGISTiledMapServiceLayer", ArcGISTiledMapServiceLayer); for each(var l:Layer in map.layers) { if (l is ArcGISTiledMapServiceLayer) { var ctl:ArcGISTiledMapServiceLayer = deepClone(l) as ArcGISTiledMapServiceLayer; cloneMap.addLayer(ctl); } } } private function deepClone ( obj : * ) : * { var bytes : ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject(); } ]]> </fx:Script> <fx:Declarations> <esri:Extent id="initialExtent" xmin="-13635000" ymin="4541000" xmax="-13625000" ymax="4547000"> <esri:SpatialReference wkid="102100"/> </esri:Extent> </fx:Declarations> <esri:Map id="map" x="12" y="57" width="376" height="230" extent="{initialExtent}"> <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" alpha=".5"/> <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer" alpha=".5"/> </esri:Map> <esri:Map id="cloneMap" x="506" y="52" width="399" height="269" extent="{initialExtent}"/> <s:Button x="210" y="347" label="Clone" click="button1_clickHandler(event)"/> </s:Application>