Creating a static image of a map

You can create a static image of a map by invoking MapField.getImage(), which is provided in the net.rim.device.api.lbs.maps.ui package, or MapFactory.generateStaticImage(), which is provided in the net.rim.device.api.lbs.maps package.

You can invoke MapField.getImage() to capture an image of the current map view on the screen, including any data that is visible on the map. MapField.getImage() is used to capture images in UI applications, where the map is a field on the screen. For example, in an application that displays a map of points of interest, you can provide a button that BlackBerry device users can click, to save an image of the current map.

You can invoke MapFactory.generateStaticImage() to create an image of a map in the following situations:
  • To create an image of the current map view on the screen in a non-UI application (for example, in an application that sends periodic updates of a person’s location to an email address)
  • To create an image of a map that does not allow user interaction such as panning and zooming (for example, in a contacts application to provide an image of a map for the contact’s home address)

The generateStaticImage() methods in the MapFactory class provide control over the coordinates for the center and the zoom level of the image, and calculate the coordinates based on the mappable items that are provided.

Method

Description

generateStaticMapImage(MapDimensions mapProperties, MappableVector data)

This method uses a MappableVector class and provides the application with control over the coordinates for the center and the zoom level of the map image.

generateStaticMapImage(MapDimensions mapProperties, MapDataModel data)

This method uses a MapDataModel class and provides the application with control over the coordinates for the center and the zoom level of the map image.

generateStaticMapImage(XYDimension imageSize, MappableVector data)

This method calculates the center and zoom level of the image based on the mappable data in the MappableVector.

Code sample: Creating a static image of a map in a UI application

// add the data to a collection
MapDataModel data = new MapDataModel();
data.add( (Mappable) new MapLocation( 43.47550, -80.53900, "Andrew", null ) );
data.add( (Mappable) new MapLocation( 43.48261, -80.54169, "Blake", null ) );
data.add( (Mappable) new MapLocation( 43.47751, -80.54817, "Christine", null ) );

// create the map and specify the map size
MapField map = new MapField(data, 200, 200);

// create the image
Bitmap image = map.getImage();

Code sample: Creating a static image of a map (map center and zoom level are calculated)

// add the data to a collection
MappableVector data = new MappableVector();
data.addElement( new MapLocation( 43.47550, -80.53900, "Andrew", null ) );
data.addElement( new MapLocation( 43.48261, -80.54169, "Blake", null ) );
data.addElement( new MapLocation( 43.47751, -80.54817, "Christine", null ) );

// specify the size of the resulting image
XYDimension imageSize = new XYDimension( 200, 100 );
        
// create the image
Bitmap map = MapFactory.getInstance().generateStaticMapImage( imageSize, data );

Code sample: Creating a static image of a map (map center and zoom level are specified)

// add the data to a collection
MapDataModel data = new MapDataModel();
MapLocation andrew = new MapLocation(43.47550, -80.53900, "Andrew", null );
data.add( (Mappable) andrew );
data.add( (Mappable) new MapLocation( 43.48261, -80.54169, "Blake", null ) );
data.add( (Mappable) new MapLocation( 43.47751, -80.54817, "Christine", null ) );
// visibility for this location is false and it will not display on the map
data.add( (Mappable) new MapLocation( 43.49487, -80.55335, "Dustin", null), null, false );
        
// specify the image size, center and zoom level
MapDimensions dim = new MapDimensions( 200, 100 );
dim.setCentre( andrew );
dim.setZoom( 3 );
        
// create the image
Bitmap map = MapFactory.getInstance().generateStaticMapImage( dim, data );

Was this information helpful? Send us your comments.