Help Center

Local Navigation

Start a BlackBerry device application from BlackBerry Maps

You can add a menu item to BlackBerry® Maps that permits a BlackBerry device user to start a BlackBerry device application using location information. The BlackBerry device user can also change location information and send the information to the BlackBerry device application.
  1. Open the BlackBerry® Integrated Development Environment.
  2. Create a project.
  3. Right-click the project and click Properties.
  4. On the Applications tab, in the Arguments passed to field, type startup . Make sure that the value you type matches the value in the startsWith argument in the main method of the BlackBerry device application.
  5. Select Auto-run on startup.
  6. Select System module.
  7. Click OK.
  8. Import the following classes:
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.blackberry.api.invoke.*;
    import net.rim.device.api.system.*;
    import net.rim.blackberry.api.maps.*;
    import net.rim.blackberry.api.menuitem.*;
    
  9. Create a class that extends the ApplicationMenuItem class and implements the run() and toString() methods.
    private static class MapsMenuItem extends ApplicationMenuItem
        {
            MapsMenuItem()
            {         
                super(20);
            }
          
         public String toString()
            {
                return "Menu Item Demo";
            }        
    
            public Object run(Object context)
            {
                if (context instanceof MapView )
                { 
                    _mv = (MapView)context;                
                    UiApplication app = UiApplication.getUiApplication();
                    app.pushScreen( new MapsMenuItemScreen(_mv) );  
                    app.requestForeground();      
                }
                return null;
            }
        }    
    }
    
  10. In the main() method, register a menu item with BlackBerry Maps.
    public static void main(String[] args) 
    {
     if (args != null && args.length > 0)
     {
      if (args[0].equals("startup"))
      {              
       ApplicationMenuItemRepository amir = ApplicationMenuItem
        Repository.getInstance();
    
       ApplicationDescriptor artup = ApplicationDescriptor
        .currentApplicationDescriptor();
    
       ApplicationDescriptor ad_gui = new ApplicationDescriptor
        (ad_startup , "gui", new String[]{"gui"});
    
       amir.addMenuItem(ApplicationMenuItemRepository.
        MENUITEM_MAPS , new MapsMenuItem() , ad_gui);                
       }
     
      else if (args[0].equals("gui"))
      {                
       MapsMenuItemDemo app = new MapsMenuItemDemo();
       app.enterEventDispatcher();                
      }
    
     }
    } 
    
  11. Create a screen for the application.
    final class MapsMenuItemScreen extends MainScreen
    {    
    private MapView _mapview;        
    private BasicEditField _latitudeField;
    private BasicEditField _logitudeField;
    private NumericChoiceField _zoomField;  
    
    MapsMenuItemScreen(MapView _mv)
    {      
     _mapview = _mv;        
            
     _latitudeField = new BasicEditField ("Latitude: " , _mv.getLatitude()
        /100000.0 + "" , 9 , BasicEditField.FILTER_REAL_NUMERIC );
      
     _logitudeField = new BasicEditField ("Longitude: ",_mv.getLongitude()
       /100000.0 + "" , 10 , BasicEditField.FILTER_REAL_NUMERIC);
     
     _zoomField = new NumericChoiceField ("Zoom: " , 0 , 
       MapView.MAX_ZOOM , 1 , _mv.getZoom()); 
            
     add(_latitudeField);
     add(_logitudeField);        
     add(_zoomField); 
     add(new SeparatorField());
     add(new RichTextField("Edit latitude, longitude and zoom level settings
       and select View Map from the menu." , Field.NON_FOCUSABLE));       
     
     addMenuItem(viewMapItem);
     setTitle("Location Details Screen");    
     }
    
  12. Create a menu item that displays the latitude, longitude, and zoom values from the MapView context object.
    private MenuItem viewMapItem = new MenuItem("View Map", 1000, 10) 
    {
    public void run()
     {            
     _mapview.setZoom( _zoomField.getSelectedValue() );
      
       try
       {
        int latitude = (int) (100000 * Double.parseDouble
         (_latitudeField.getText()));
     
        int longitude = (int) (100000 * Double.parseDouble
         (_logitudeField.getText()));
     
        if (latitude > 9000000 || latitude < -9000000 ||
          longitude >= 18000000 || longitude < -18000000)
        {
         throw new IllegalArgumentException ();
        }
    
        _mapview.setLatitude(latitude);
        _mapview.setLongitude(longitude);
                 
        Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, 
         new MapsArguments(_mapview));
       
        close();
       }
    
       catch(RuntimeException re)
       {
        Dialog.alert("Invalid Longitude and/or Latitude");
        _latitudeField.setFocus();
        }
      }
    };
    
  13. Populate the menu.
    protected void makeMenu( Menu menu, int instance ) 
        {
            super.makeMenu( menu, instance );        
            if(getFieldWithFocus() == _zoomField)
            {
                menu.setDefault(1);
            }
    }
    

Was this information helpful? Send us your comments.