Saturday, February 21, 2009

How To - Add an ApplicationMenuItem to BlackBerry Maps


The BlackBerry Maps application allows the BlackBerry device user to view maps and receive directions on a BlackBerry device. BlackBerry Maps can also provide a visual route and navigational instructions to a specified destination.

BlackBerry Maps works with BlackBerry Device Software 4.1 and later. BlackBerry Maps is included with BlackBerry Device Software 4.2 and later. For use with BlackBerry Device Software 4.1, BlackBerry Maps can be downloaded from www.blackberry.com/maps.

With third-party applications, an ApplicationMenuItem can be added to the menu of BlackBerry Maps and pass a MapView object into the application.

This article focuses on adding an ApplicationMenuItem to BlackBerry Maps. Please see How To - Add a custom menu item to an existing BlackBerry application for information on the way to add a custom menu item to other BlackBerry device applications.

An ApplicationDescriptor is required for adding an ApplicationMenuItem to BlackBerry Maps. The code for registering a menu item with the BlackBerry Maps application is as follows:

ApplicationMenuItemRepository amir =
ApplicationMenuItemRepository.getInstance();
ApplicationDescriptor ad_startup =
ApplicationDescriptor.currentApplicationDescriptor();
ApplicationDescriptor ad_gui =
new ApplicationDescriptor(ad_startup, "gui", null);
amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_MAPS,new MapMenuItem(),ad_gui);

The menu item MapMenuItem() is then registered with the BlackBerry Maps application (MENUITEM_MAPS) to invoke the current application with ad_gui as the ApplicationDescriptor. The MapMenuItem()method contains the actual code for specifying the menu item as shown below. MapMenuItem implements the abstract class ApplicationMenuItem and implements the run and toString methods. The toString method specifies the text to display on the menu item.

private static class MapMenuItem extends ApplicationMenuItem {
MapMenuItem() {
super(20);
}
public String toString() {
return "My Menu Item";
}
public Object run(Object context) {
mv = (MapView)context;
if (mv != null) {
System.out.println("Latitude = " + mv.getLatitude());
System.out.println("Longitude = " + mv.getLongitude());
System.out.println("Zoom = " + mv.getZoom());
}
else {
throw new IllegalStateException("Context is null, expected a MapView instance");
}
return null;
}
}

No comments:

Post a Comment

Place your comments here...