Retrieving the estimated travel time, distance, and departure time
You can create an application that requests the estimated travel time, distance, and departure time for automobile travel in the United States and Canada by using the Travel Time API, which is provided in the net.rim.device.api.lbs.travel package. For example, you can create a social networking application that provides the BlackBerry device user with the estimated time of arrival at a friend's location. You could also create an application that integrates with a user's Calendar application to provide the user with a departure time for getting to an upcoming appointment.
TravelTimeEstimator is a singleton class that supports synchronous and asynchronous requests. Synchronous requests block processes on the current thread until the request returns or throws an exception. As a best practice, you should run synchronous calls on a separate thread, so that the request doesn't block the current thread. An asynchronous request returns to the thread after sending a request for an estimate. The results are returned asynchronously to a listener object that you provide.
Requests are sent to a travel time server, which uses current and historical traffic information to plot a route between the starting and ending locations. You can create a request for the travel time and distance by obtaining an instance of the TravelTimeEstimator class, invoking TravelTimeEstimator.requestArrivalEstimate() and passing in the geographic coordinates for the starting and ending locations, and the start time. When the request returns a TravelTime object, you can retrieve the travel time and distance by invoking TravelTime.getElapsedTime() and TravelTime.getDistance(), respectively. Requesting a departure time requires that you invoke TravelTimeEstimator.requestDepartureEstimate() and pass in the arrival time along with the coordinates for the starting and ending locations. When the request returns, you can retrieve the departure time by invoking TravelTime.getStartTime().
Retrieve the estimated travel time and distance
You can create an application that retrieves the estimated time and distance that it takes to travel between two locations. In the following steps, you create an application that contains a text field in which the BlackBerry device user types a destination address, a button that the user clicks to retrieve the estimated time and distance to reach the destination, and fields to display the results. The request for the estimated travel time and distance is sent synchronously.
Make sure that the BlackBerry device or BlackBerry Smartphone Simulator has GPS enabled.
Code sample: Retrieving the estimated travel time and distance
import net.rim.device.api.lbs.maps.MapDimensions;
import net.rim.device.api.lbs.maps.model.MapLocation;
import net.rim.device.api.lbs.maps.model.MapPoint;
import net.rim.device.api.lbs.maps.server.Geocoder;
import net.rim.device.api.lbs.maps.server.exchange.GeocodeExchange;
import net.rim.device.api.lbs.travel.TravelTime;
import net.rim.device.api.lbs.travel.TravelTimeEstimator;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;
import java.util.Vector;
import javax.microedition.location.Coordinates;
import javax.microedition.location.LocationProvider;
public final class TravelTimeDemo extends UiApplication
{
public static void main(String[] args)
{
TravelTimeDemo theApp = new TravelTimeDemo();
theApp.enterEventDispatcher();
}
public TravelTimeDemo()
{
pushScreen(new TravelTimeScreen());
}
}
class TravelTimeScreen extends MainScreen implements FieldChangeListener
{
private BasicEditField _destinationField;
private LabelField _timeLabel;
private LabelField _distanceLabel;
private ButtonField travelButton;
public TravelTimeScreen()
{
super(DEFAULT_CLOSE | DEFAULT_MENU);
setTitle("Travel Time Demo");
_destinationField = new BasicEditField("Destination: ", "", 500,
TextField.NO_NEWLINE);
add(_destinationField);
_timeLabel = new LabelField();
add(_timeLabel);
_distanceLabel = new LabelField();
add(_distanceLabel);
travelButton = new ButtonField("Get Travel Estimate",
ButtonField.CONSUME_CLICK);
travelButton.setChangeListener(this);
add(travelButton);
}
public void fieldChanged(final Field field, int context)
{
final String destination = _destinationField.getText();
if ((destination == null) || (destination.length() == 0))
{
Dialog.alert("Destination field cannot be empty");
return;
}
_timeLabel.setText("");
_distanceLabel.setText("");
Thread travelTimeThread = new Thread()
{
public void run()
{
try
{
LocationProvider provider = LocationProvider.getInstance(null);
if (provider == null)
{
throw new IllegalStateException("No LocationProvider
available");
}
Coordinates startCoords = provider.getLocation(-1)
.getQualifiedCoordinates();
MapPoint startPoint = new MapPoint(startCoords);
GeocodeExchange exchange = Geocoder.getInstance().geocode(null,
destination,
new MapDimensions(startPoint,480, 360, 5, 0), 0);
if(exchange.getExceptionList().size() < 0)
{
throw new IllegalStateException("Can't find end
coordinates.");
}
Vector results = exchange.getResults();
MapLocation location = (MapLocation) results.elementAt(0);
Coordinates endCoords = new Coordinates(location.getLat(),
location.getLon(), 0);
TravelTimeEstimator est = TravelTimeEstimator.getInstance();
final TravelTime travelTime = est.requestArrivalEstimate(
startCoords, endCoords, TravelTime.START_NOW, null);
showResults(travelTime);
}
catch (final Exception e)
{
Dialog.alert(e.getMessage());
}
}
};
travelTimeThread.start();
}
private void showResults(final TravelTime travelTime)
{
Application.getApplication().invokeLater(new Runnable()
{
public void run()
{
long value = travelTime.getElapsedTime() / 1000;
long seconds = value % 60;
value /= 60;
long minutes = value % 60;
long hours = value / 60;
StringBuffer buffer = new StringBuffer();
buffer.append(hours);
buffer.append(':');
if (minutes < 10)
{
buffer.append('0');
}
buffer.append(minutes);
buffer.append(':');
if (seconds < 10)
{
buffer.append('0');
}
buffer.append(seconds);
String msg = "Travel Time (h:m:s): " + buffer.toString();
TravelTimeScreen.this._timeLabel.setText(msg);
double distance = travelTime.getDistance() / 1000.0;
msg = "Distance (km): " + Double.toString(distance);
TravelTimeScreen.this._distanceLabel.setText(msg);
}
});
}
}