Embed a map in an application
You can embed and control a map in a BlackBerry® device application by using the MapField class.
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);
}
}
}
}
Parent topic: Embedding a map in an application