Code sample: Receiving promity alerts using geofencing
The following code sample demonstrates how to define two geofenced areas, and listen for geofencing events. The perimeter for the first location is defined by a set of coordinates, along with a radius. The second location is defined by a set of coordinates representing a polygon. In a real-world scenario, you could use this...
import net.rim.device.api.gps.*;
import net.rim.device.api.location.*;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import javax.microedition.location.*;
public class GeofencingDemo extends UiApplication
{
public static void main(String args[])
{
GeofencingDemo app = new GeofencingDemo();
app.enterEventDispatcher();
}
public GeofencingDemo()
{
pushScreen(new GeofencingDemoScreen());
}
private class GeofencingDemoScreen extends MainScreen
{
private Geofence geofence;
private GFListener gfListener;
public GeofencingDemoScreen()
{
setTitle("Geofencing Demo");
geofence = new Geofence();
gfListener = new GFListener();
Coordinates coords = new Coordinates(43.2750, -80.3318, 0);
Coordinates[] coords2 = new Coordinates[3];
coords2[0] = new Coordinates(43.2851, -80.3229, 0);
coords2[1] = new Coordinates(43.2945, -80.3220, 0);
coords2[2] = new Coordinates(43.2852, -80.3199, 0);
geofence.monitorPerimeter(gfListener, "RIM 12", coords, 1000, 10, -1);
geofence.monitorPerimeter(gfListener, "RIM campus", coords2, 10, -1);
}
}
private class GFListener implements GeofenceListener
{
public void perimeterEntered(String tag, BlackBerryLocation location)
{
// Code that executes when the user enters the perimeter.
}
public void perimeterExited(String tag, BlackBerryLocation location)
{
// Code that executes when the user leaves the perimeter.
}
public void errorOccurred(int errorCode)
{
// Code that runs when an error occurs.
}
}
}