Exemplo de código: Atualizar e reprocessar objetos em um mapa
O seguinte exemplo de código ilustra como criar um objeto dinâmico mapeável, adicionar o objeto a um mapa e dinamicamente atualizar a localização do objeto. Neste exemplo de código, o aplicativo gera novas coordenadas para o objeto mapeável dinâmico cada vez que um usuário pressiona o botão na tela. Geralmente estes dados seriam obtidos solicitando uma localização atual do usuário ou obtendo informações de localização de um serviço Web.
/*
* 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;
}
}
Próximo tópico: Personalizar a aparência de um mapa
Tópico anterior: Atualizar e reprocessar objetos em um mapa
Estas informações foram úteis? Gostaríamos de receber seus comentários.