Code sample: Updating and re-rendering objects on a map
The following code sample illustrates how to create a dynamic mappable object, add the object to a map, and dynamically update the location of the object. In this code sample, the application generates new coordinates for the dynamic mappable object each time a user presses the button on the screen. Typically this data would be obtained by requesting a user's current location, or by obtaining location information from a web service.
/*
* DynamicMappableApp.java
*/
import net.rim.device.api.lbs.maps.*;
import net.rim.device.api.lbs.maps.model.*;
import net.rim.device.api.lbs.maps.ui.*;
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.*;
import java.util.Random;
public class DynamicMappableApp extends UiApplication
{
public static final void main( String[] args )
{
new DynamicMappableApp().enterEventDispatcher();
}
public DynamicMappableApp()
{
pushScreen( new DynamicMappableScreen() );
}
private class DynamicMappableScreen extends FullScreen implements
FieldChangeListener
{
private RichMapField map;
private MapDataModel model;
private UpdatableMappable mappableObject;
private ButtonField button;
private DynamicMappableScreen()
{
super(FullScreen.DEFAULT_CLOSE | FullScreen.DEFAULT_MENU);
// Create the map.
map = MapFactory.getInstance().generateRichMapField();
map.getAction().enableOperationMode(MapConstants.MODE_SHARED_FOCUS);
map.getMapField().setDimensions(new MapDimensions(Display.getWidth(),
Display.getHeight()-100));
map.getMapField().getAction().setCenter(new MapPoint( 45.0, -75.0));
map.getMapField().getAction().setZoom(4);
add(map);
// Create an instance of the dynamic mappable class and add it to
// the data model for the map.
mappableObject = new UpdatableMappable(45.0, -75.0, "Map Location",
"Dynamic Updates");
model = map.getModel();
model.add(mappableObject, "dynamic", true);
// Create and add a button that initiates an update.
button = new ButtonField("Update Location");
button.setChangeListener(this);
add(button);
}
public void fieldChanged (Field field, int context)
{
// Create a random value between 0 and 0.03 in steps of 0.0001
// to update the lat and lon with.
Random randomizer = new Random();
double dlat = randomizer.nextInt(300) / 10000.0;
double dlon = randomizer.nextInt(300) / 10000.0;
mappableObject.setLat(44.985 + dlat);
mappableObject.setLon(-74.985 + dlon);
// Create and trigger a MappableChangeEvent that captures the old
// and new values.
MappableChangeEvent event = new MappableChangeEvent();
event.setOldState((Mappable) new MapPoint(mappableObject.oldLat,
mappableObject.oldLon));
event.setNewState(mappableObject);
mappableObject.getEventManager().triggerEvent(event);
}
}
}
/*
* UpdatableMappable.java
*/
import net.rim.device.api.lbs.maps.model.*;
public class UpdatableMappable extends MapLocation implements DynamicMappable
{
public MappableEventManager eventManager;
// Stores the old latitude and longitude.
public double oldLat;
public double oldLon;
public UpdatableMappable(double lat, double lon, String name, String description)
{
super(lat, lon, name, description);
eventManager = new MappableEventManager();
}
public void setLon(final double lon)
{
// Record the old value before it's changed.
oldLon = getLon();
super.setLon(lon);
}
public void setLat(final double lat)
{
// Record the old value before it's changed.
oldLat = getLat();
super.setLat(lat);
}
public MappableEventManager getEventManager()
{
return eventManager;
}
}