Réception d'alertes de proximité par geofencing
Vous pouvez utiliser la classe net.rim.device.api.location.Geofence pour définir un périmètre autour d'une zone devant être surveillée par votre application. Votre application reçoit une alerte quand un utilisateur de terminal BlackBerry entre ou sort d'une zone protégée par geofencing. Le geofencing vous permet d'alerter vos utilisateurs lorsqu'ils entrent dans une zone de type point d'intérêt. Vous pouvez aussi leur envoyer des informations en rapport avec leur position.
Lorsqu'une application appelle la méthode Geofence.monitorPerimeter(), le serveur de geofencing envoie les alertes de proximité à l'application quand le terminal entre ou sort de la zone protégée par geofencing. Chaque objet Geofence peut contrôler jusqu'à 20 zones simultanément.
Définition d'une zone à surveiller
// Single Coordinates object, to be used along with a radius Coordinates coords = new Coordinates(43.2750, -80.3318, 0); // Array of three Coordinates objects, representing a three-sided polygon 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);
GFListener gfListener = new GFListener(); Geofence geofence = new Geofence(); // Starts monitoring the area defined by the coordinate and a radius geofence.monitorPerimeter(gfListener, "RIM 12", coords, 500, 50, -1); // Starts monitoring the area defined by the set of coordinates geofence.monitorPerimeter(gfListener, "RIM campus", coords2, 50, -1);
Écoute des événements de geofencing
private class GFListener implements GeofenceListener
{
public void perimeterEntered(String tag, BlackBerryLocation location)
{
// Code that runs when a user enters the perimeter
}
public void errorOccurred(int errorCode)
{
// Code that runs when an error occurs
}
public void perimeterExited(String tag, BlackBerryLocation location)
{
// Code that runs when a user leaves the perimeter
}
}
Si la méthode errorOccured() est appelée du fait du niveau faible de la batterie ou si les services de positionnement sont indisponibles (erreur OUT_OF_SERVICE), l'application ne reçoit plus de notification.
Ces informations vous ont-elles été utiles ? Envoyez-nous vos commentaires.