BlackBerry 6 Features: MapFactory

by Retired on ‎08-03-2010 11:22 AM - edited on ‎09-20-2010 05:05 PM by Retired (14,313 Views)

MapFactory is class available with BlackBerry OS 6.0 that can generate static maps and an interactive RichMapField.  The MapFactory greatly reduced the effort required to get a map into an application and no sacrifices need to be made since the underlying MapField is always accessible using RichMapField.getMapField(). 

 

Static Maps

Static maps can easily be created using the MapFactory class. Since the class will generate a static bitmap, all the configuration information must be passed at the initial creation point.  We will use the MapDimension class to specify the map dimensions, zoom level and center point.  If we have data points on the map that we need to display we can use the MapDataModel class.

 

Here is an example of how we can generate and display a static map.

 

 

// Set the properties of the map using MapDimensions
MapDimensions md = new MapDimensions(320, 320);
md.setZoom(3); //Zoom Level 3 is between city level and street level
md.setCentre(new MapPoint(45.504512, -73.557029));
 
// Set the data that we want to display on the map
MapDataModel data = new MapDataModel();
MapLocation dataPoint1 = new MapLocation(45.504512, -73.557029,
"Notre Dame Basilica", null);
 
int cId = data.add((Mappable) dataPoint1, "Basilica");
data.tag(cId, "Church"); // locations can have more than one tag
data.setVisible( "Church" ); //We can hide or show each tag
 
//Create the map using our two objects
Bitmap map = MapFactory.getInstance().generateStaticMapImage(md, data);
 
//Add the bitmap to the current manager
add(new BitmapField(map));  

 

 

 

 

The above code will produce the following screen:

 screen1.png

 

 

 

Interactive Maps

Creating a rich map is just as easy as a static map.   A rich map is an interactive map field that the user can control.  The user is able to pan and zoom within the field while new map data is streamed over the internet in real time.  Here is the code:

 

 

//Generate a RichMapField using MapFactory
RichMapField map = MapFactory.getInstance().generateRichMapField();
//Retrieve the underlying MapField so we can configure it
MapField mapField = map.getMapField();
 
//Set the properties of the map using MapDimensions
MapDimensions md = new MapDimensions(320, 320);
md.setZoom(3);
md.setCentre(new MapPoint(45.504512, -73.557029));
mapField.setDimensions(md);
 
// Set the data that we want to display on the map
MapDataModel data = new MapDataModel();
MapLocation dataPoint1 = new MapLocation(45.504512, -73.557029,
"Notre Dame Basilica", null);
 
int cId = data.add((Mappable) dataPoint1, "Basilica");
data.tag(cId, "Church"); // locations can have more than one tag
data.setVisible( "Church" );
mapField.setModel(data);
//Call mapField.update() to ensure the new map data is reflected on the display
mapField.update();
add(map);
 

 

 

 

This code will produce the following screen:

 screen2.PNG

 

Be sure to view the attached source code for a complete sample application that demonstrates the two concepts that were described above.