Draw ArcGISDynamicMapServiceLayer to BitmapData

1057
1
09-28-2011 11:38 AM
JustinRussell
New Contributor
I have a customized InfoWindow which shows data for all visible layers in a nice tree structure such that each visible service has its own branch in the tree and each layer in that service has a leaf under that branch. What I'd like to do is have the color of those layer leaf icons match the corresponding visible layers at the clicked point. I've been going about this by drawing the layer itself to a BitmapData object and grabbing a pixel from the location matching the users click on the map - then using that pixel to color the tree icon.

This seems to work fine as long as the map position remains unchanged, which is to say once I start panning/zooming I run into problems. Namely - when drawing the layers to a bitmap object after a pan I can't see anything from parts of the map that were not in the view when it initially loaded, the newly visible areas just show up as white space.

I've tried performing a validateNow() on everything from the layer itself to the entire map. I've also tried invoking refresh() on the layer before drawing it with no luck.

Any ideas? I'm also open to other suggestions for getting the color of a layer at a clicked point.
Tags (2)
0 Kudos
1 Reply
JustinRussell
New Contributor
Here's an easy way to reproduce what I'm talking about - just assign this function to your base map's mapClick event and turn on your favorite map service layer.

private function myMap_ClickHandler(event:MapMouseEvent):void {
[INDENT]// Initialize BitmapData to same size as map itself
var bmd:BitmapData = new BitmapData(myMap.width, myMap.height);

// Grab desired layer
var sl:ArcGISDynamicMapServiceLayer = myMap.getLayer("myServiceLayerId") as ArcGISDynamicMapServiceLayer;

// Draw layer to BitmapData
bmd.draw(sl);

// Convert BitmapData to a ByteArray
var encoder:JPEGEncoder = new JPEGEncoder(100);
var ba:ByteArray = encoder.encode(bmd);

// Save our ByteArray locally as an image for inspection
var fr:FileReference = new FileReference();
fr.save(ba, "MyServiceLayer.jpg");[/INDENT]
}


Here's an example of output generated on my machine using a zip codes layer:

Zip Codes at initial state
Zip Codes after pan

I simply added the zip codes layer to my map - clicked and saved the drawn layer, then panned southwest and clicked to save again. As you can see the only part of the second image that is not whited out is that which was in the initial image - and, interestingly enough, it's still in the bottom left of the generated image rather than the top right as it is on screen.

If anybody could confirm this behavior or point out where I'm going astray I'd much appreciate it.
0 Kudos