In Situm Platform, a building can have a number of Geofences. Geofences are polygonal areas that are delimited for a number of purposes: showing a message to the user when she enters, triggering an alarm if an area is too crowded, analysing the time the users spend in each area, etc. 


For detailed information on geofences, you may read their introductory description and how to create them in Situm Dashboard. You may also find interesting their data models as provided by Situm REST API Geofence methods and Situm SDK Geofence model.



Fetching all the Geofences of a building


The following code snippet shows how to retrieve the geofences  of a certain building and output their information on the application Log. For this example, we will use building 8377, which we know from a previous example.


//Fetches all the geofences from the building 8377
Building building = new Building.Builder().identifier("8377").build();
SitumSdk.communicationManager().fetchGeofencesFromBuilding(building, new Handler<List<Geofence>>() {
    @Override
    public void onSuccess(List<Geofence> geofences) {
        for (Geofence geofence: geofences){
            Log.i(TAG, "BuildingID: "+ geofence.getBuildingIdentifier());
            Log.i(TAG, "FloorID: "+ geofence.getFloorIdentifier());
            Log.i(TAG, "GeofenceID: "+ geofence.getIdentifier());

            Log.i(TAG, "Name: "+ geofence.getName());
            Log.i(TAG, "InfoHTML: "+ geofence.getInfoHtml());
            Log.i(TAG, "CustomFields: "+ geofence.getCustomFields());

            Log.i(TAG, "PolygonPoints: ");
            for (Point p: geofence.getPolygonPoints()){
        Log.i(TAG, "   Point:");
        Log.i(TAG, "       BuildingID: " + p.getBuildingIdentifier());
        Log.i(TAG, "       FloorID: " + p.getFloorIdentifier());
        Log.i(TAG, "       WSG84 Coordinate. Lat=" + p.getCoordinate().getLatitude() + " Lng="+p.getCoordinate().getLongitude());
        Log.i(TAG, "       IsIndoor? " + p.isIndoor());
            }
        }
    }

    @Override
    public void onFailure(Error error) {
Log.e(TAG, "Error: " + error );
    }
});


In the previous code snippet, we are receiving a List of Geofence objects (the geofences in Building 8377), and we loop over them to print their properties. Your application logs should be something like this: 



...
BuildingID: 8377
FloorID: 20541
GeofenceUUID: dc7a83da-cb3b-4476-9f0c-9c75e97bc98e
Name: Entrance
InfoHTML: <p><strong>Welcome!</strong></p>
CustomFields: {show_in_app=true}
PolygonPoints: 
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87247616887151 Lng=-8.562831151227792
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87246158665294 Lng=-8.56293384594478
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.872350236963406 Lng=-8.562851147272264
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87247616887151 Lng=-8.562831151227792
       IsIndoor? true
...


Let's examine them in detail.


Building and floor Identifier 


First, you will notice that the Geofence data object allows you to retrieve the identifier of the building & floor to which the geofence belongs. This is handy if you need this information later in your code. 


BuildingID: 8377
FloorID: 20541


Geofence Identifier (UUID)


Then, we output the geofence identifier. Unlike other cartography identifiers (such as building, floor or POI identifiers), the geofence identifier comes in UUID format. UUID format that all new entities introduced in Situm (such as geofences) follow. Other identifier formats are considered legacy and will eventually be deprecated.  


GeofenceUUID: dc7a83da-cb3b-4476-9f0c-9c75e97bc98e


The geofence identifier is automatically generated by Situm Platform when the geofence is created. Another way to know the identifier of a floor is by retrieving the geofences from Situm REST API.  


Geofence Name, free HTML information and custom fields


After that, we see the geofence name, its free HTML info (filled using our Rich Text Editor) and the POI's custom fields. 


Name: Entrance
InfoHTML: <p><strong>Welcome!</strong></p>
CustomFields: {show_in_app=true}


All this information can be configured when creating/editing the geofence:







Geofence Polygon 


Last but not least, each geofence will have ocupy a certain area defined as a polygon. This polygon is defined as a List of Situm Points, where:

  1. Each point represents a vertex of the polygon.
  2. The first & last points are the same point.  Therefore, a triangle will have 4 points (1 repeated), a rectangle 5 points, and so on. This is to ensure that the list of points represents a closed surface.


PolygonPoints: 
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87247616887151 Lng=-8.562831151227792
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87246158665294 Lng=-8.56293384594478
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.872350236963406 Lng=-8.562851147272264
       IsIndoor? true
   Point:
       BuildingID: 8377
       FloorID: 20541
       WSG84 Coordinate. Lat=42.87247616887151 Lng=-8.562831151227792
       IsIndoor? true



In this example, this is the geofence that has been output in the log.