MapQuest Flex / AS3 API - How to retrieve custom icons from the map and use them elsewhere

Posted by steve.walter on Sat, 02/23/2008 - 17:46 in

The trouble I ran into with the MapQuest Flex API is that any objects you create in Adobe's Flex are pass by reference, therefore retrieving custom icons from the Map Object can be head scratching. How can you retrieve the custom icons being displayed on the map for use in a custom control such as a map legend without removing them from the map itself?

Without a built in copy function to retrieve the MapIcon object and use it elsewhere here is a simple little trick to "copy" the Custom Icon displayed on the map. As a matter of fact this will retrieve any icons associated with a poi on the Map Object using a little bit of bitmap drawing functionality built into Flex.

****************************************************************

Retrieve the Image component from the Poi you would like to copy the icon from:

//Create a Display Object from the Image Component Of the Poi MapIcon

var mi:DisplayObject = poi.getIcon().getImage();

// Create a BitMap Data Object with the following parameters
// Width of the Image
// Height of the Image
// Transparency Support = True
// Fill Color of the Bitmap Data - can be anything will be replaced by the icon image

var bd:BitmapData = new BitmapData(mi.width, mi.height, true, 0x00ffffff);

// Draw the contents of the Display Object onto the BitMapData Object

bd.draw(mi);

//Create a Bitmap from the BitMapData

var poiImage:Bitmap = new Bitmap(bd);

****************************************************************

There you have it, now you can create a Map Legend using custom icons used in your map pois without having your map icons dissapear.

Reply
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.