Embed a map in an application

You can embed and control a map in a BlackBerry® device application by using the MapField class.
  1. Import the required classes and interfaces.
    import net.rim.device.api.lbs.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.system.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. The myScreen class represents the custom screen, which is described in step 3.
    public class MapFieldDemo extends UiApplication
    {
        public static void main(String[] args)
        {
            MapFieldDemo theApp = new MapFieldDemo();
            theApp.enterEventDispatcher();
        }
    
        MapFieldDemo()
        {
            pushScreen(new myScreen());
        }
    }
  3. Create the framework for the screen by extending the FullScreen class. Implement the FieldChangeLister interface to listen for events from the ButtonField object which is described in step 5. In the constructor, invoke super() to create a default menu. Invoke createUI() to create the UI, which is described in step 4.
    class myScreen extends FullScreen implements FieldChangeListener
    {
        private MapField _mapField;
        private ButtonField _zoomIn;
        private ButtonField _zoomOut;
        private ButtonField _moveTo;
        private BasicEditField _moveByText;
        private BasicEditField _testsOutput;
        private ObjectChoiceField _moveUnits;
        private int _prevLat =  4328915;
        private int _prevLon = -8032480;
        private int _prevZoom = 4;
    
        myScreen()
        {
            super(DEFAULT_MENU | DEFAULT_CLOSE);
            createUI();
        }
    }
  4. In the myScreen class, create a method that you can use to create the UI within a synchronized block. Create an instance of the VerticalFieldManager class to lay out the fields on the screen in a single column. Create an instance of the MapField class to create a map in BlackBerry® Maps. Invoke add() to add the MapField to the VerticalFieldManager.
    private void createUI()
    {
        VerticalFieldManager vfm;
    
        synchronized (Application.getEventLock())
        {
            vfm = new VerticalFieldManager
              (Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
            _mapField = new MapField();
            _mapField.setPreferredSize(_mapField.getPreferredWidth(),
              (int) (Display.getHeight() * 0.66));
            vfm.add(_mapField);
        }
    }
  5. In createUI, create UI components so that the user can interact with the map. For each UI component, invoke Field.setChangeListener() to specify the listeners for the UI components. Invoke add() to add the UI components to the screen.
    FlowFieldManager ffm = new FlowFieldManager();
    ffm.add(_zoomIn = new ButtonField("Zoom In"));
    _zoomIn.setChangeListener(this);
    
    ffm.add(_zoomOut = new ButtonField("Zoom Out"));
    _zoomOut.setChangeListener(this);
    
    ffm.add(_moveUnits = new ObjectChoiceField("Units: ", new String[]
    {
        "pixels", "degrees"
    }));
    ffm.add(_moveByText = new BasicEditField("Move horizontal, vertical: ",
        "", 20, BasicEditField.NO_NEWLINE));
    
    ffm.add(_moveTo = new ButtonField("Move"));
    _moveTo.setChangeListener(this);
    
    vfm.add(ffm);
    add(vfm);
  6. Set the default coordinates and the default zoom settings for the embedded map by invoking MapField.moveTo() and MapField.setZoom().
    _mapField.moveTo(_prevLat, _prevLon);
    _mapField.setZoom(_prevZoom);
  7. In the myScreen class, implement FieldChangeListener.fieldChanged() to handle the user's interactions with the UI components on the screen.
    public void fieldChanged(Field field, int context)
    {
        if (field == _zoomIn)
        {
            _mapField.setZoom(Math.max(_mapField.getZoom() - 1,
              _mapField.getMinZoom()));
        }
        else if (field == _zoomOut)
        {
            _mapField.setZoom(Math.min(_mapField.getZoom() + 1,
              _mapField.getMaxZoom()));
        }
        else if (field == _moveTo)
        {
            String amount = _moveByText.getText();
            int ix = amount.indexOf(",");
            int horizontalDelta = 0;
            int verticalDelta = 0;
    
            if (ix == -1)
            {
                ix = amount.indexOf(" ");
            }
    
            try
            {
                horizontalDelta = Integer.parseInt
                  (amount.substring(0, ix).trim());
                verticalDelta = Integer.parseInt
                  (amount.substring(ix + 1, amount.length()).trim());
            }
            catch (NumberFormatException nfe)
            {
                Dialog.alert("Bad value!");
            }
    
            if (_moveUnits.getSelectedIndex() == 0)
            {
                _mapField.move(horizontalDelta, verticalDelta);
            }
            else
            {
                horizontalDelta += _mapField.getLongitude();
                verticalDelta += _mapField.getLatitude();
                _mapField.moveTo(verticalDelta, horizontalDelta);
            }
        }
    }

Code sample: Embedding a map in an application

import net.rim.device.api.lbs.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public class MapFieldDemo extends UiApplication
{
    public static void main(String[] args)
    {
        MapFieldDemo theApp = new MapFieldDemo();
        theApp.enterEventDispatcher();
    }

    MapFieldDemo()
    {
        pushScreen(new myScreen());
    }
}

class myScreen extends FullScreen implements FieldChangeListener
{
    private MapField _mapField;
    private ButtonField _zoomIn;
    private ButtonField _zoomOut;
    private ButtonField _moveTo;
    private BasicEditField _moveByText;
    private BasicEditField _testsOutput;
    private ObjectChoiceField _moveUnits;
    private int _prevLat =  4328915;
    private int _prevLon = -8032480;
    private int _prevZoom = 4;

    myScreen()
    {
        super(DEFAULT_MENU | DEFAULT_CLOSE);
        createUI();
    }

    private void createUI()
    {
        VerticalFieldManager vfm;

        synchronized (Application.getEventLock())
        {
            vfm = new VerticalFieldManager
              (Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
            _mapField = new MapField();
            _mapField.setPreferredSize(_mapField.getPreferredWidth(),
              (int)(Display.getHeight() * 0.66));
            vfm.add(_mapField);
        }

        FlowFieldManager ffm = new FlowFieldManager();
        ffm.add(_zoomIn = new ButtonField("Zoom In"));
        _zoomIn.setChangeListener(this);

        ffm.add(_zoomOut = new ButtonField("Zoom Out"));
        _zoomOut.setChangeListener(this);

        ffm.add(_moveUnits = new ObjectChoiceField("Units: ", new String[]
        {
            "pixels", "degrees"
        }));
        ffm.add(_moveByText = new BasicEditField("Move horizontal, vertical: ",
          "", 20, BasicEditField.NO_NEWLINE));

        ffm.add(_moveTo = new ButtonField("Move"));
        _moveTo.setChangeListener(this);

        vfm.add(ffm);
        add(vfm);

        _mapField.moveTo(_prevLat, _prevLon);
        _mapField.setZoom(_prevZoom);
    }

    public void fieldChanged(Field field, int context)
    {
        if (field == _zoomIn)
        {
            _mapField.setZoom(Math.max(_mapField.getZoom() - 1,
              _mapField.getMinZoom()));
        }
        else if (field == _zoomOut)
        {
            _mapField.setZoom(Math.min(_mapField.getZoom() + 1,
              _mapField.getMaxZoom()));
        }
        else if (field == _moveTo)
        {
            String amount = _moveByText.getText();
            int ix = amount.indexOf(",");
            int horizontalDelta = 0;
            int verticalDelta = 0;

            if (ix == -1)
            {
                ix = amount.indexOf(" ");
            }

            try
            {
                horizontalDelta = Integer.parseInt
                  (amount.substring(0, ix).trim());
                verticalDelta = Integer.parseInt
                  (amount.substring(ix + 1, amount.length()).trim());
            }
            catch (NumberFormatException nfe)
            {
                Dialog.alert("Bad value!");
            }

            if (_moveUnits.getSelectedIndex() == 0)
            {
                _mapField.move(horizontalDelta, verticalDelta);
            }
            else
            {
                horizontalDelta += _mapField.getLongitude();
                verticalDelta += _mapField.getLatitude();
                _mapField.moveTo(verticalDelta, horizontalDelta);
            }
        }
    }
}

Was this information helpful? Send us your comments.