Android code samples

Code samples for supporting you during Woosmap Geofencing Android SDK integration.

  1. Monitoring a custom geofence with live tracking profile enabled
  2. Monitoring Woosmap assets with the passive tracking profile enabled

Monitoring a custom geofence with live tracking profile enabled

Description of the use case: Continuously track the user’s location and monitor a custom geofence by manually adding it using the addGeofence() method. The RegionLogReadyCallback callback will be triggered upon geofence entry/exit events.

java
        package com.companyname.appname;

import android.location.Location;
import android.os.Bundle;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.model.LatLng;
import com.webgeoservices.woosmapgeofencing.Woosmap;

import com.webgeoservices.woosmapgeofencingcore.database.*;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;

    private Woosmap woosmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Instanciate woosmap object
        this.woosmap = Woosmap.getInstance().initializeWoosmap(this);

        this.woosmap.setRegionLogReadyListener( new WoosRegionLogReadyListener() );
        this.woosmap.setLocationReadyListener( new WoosLocationReadyListener() );

        // Passive tracking - Soft battery consumption
        this.woosmap.startTracking(Woosmap.ConfigurationProfile.liveTracking);

        // Create a custom geofence. Define the center of the geofence (lat/lng) with a radius (in meters)
        Woosmap.getInstance().addGeofence( "test", new LatLng(43.6066262, 3.921564625), 100, "circle");

    }

    public class WoosRegionLogReadyListener implements Woosmap.RegionLogReadyListener {

        // The RegionLogReadyCallback callback is triggered when the user enters (or exits) in a geofence
        public void RegionLogReadyCallback(RegionLog regionLog) {
            // use regionLog.eventName value to know if the user enters or exits the geofence
            Log.d("WoosmapGeofencing", "eventName:"+ regionLog.eventName + " - regionLog: " + regionLog.id);
        }
    }

    public class WoosLocationReadyListener implements Woosmap.LocationReadyListener {

        // The LocationReadyCallback callback is triggered when a new user location is collected by the SDK
        public void LocationReadyCallback(Location location) {
            Log.d("WoosmapGeofencing", "User location collected - lat: " + location.getLatitude() + ", lng: " + location.getLongitude());

        }
    }

    @Override
    public void onStart() { super.onStart(); }

    @Override
    public void onResume() { super.onResume(); woosmap.onResume(); }

    @Override
    public void onPause() { super.onPause(); woosmap.onPause();}

    @Override
    protected void onDestroy() { woosmap.onDestroy(); super.onDestroy(); };
}

    

Monitoring Woosmap assets with the passive tracking profile enabled

Description of the use case: Silently track the user’s location and monitor Woosmap assets that have been previously uploaded in the Woosmap platform. The RegionLogReadyCallback callback will be triggered upon geofence entry/exit events.

java
        package com.companyname.appname;

import android.location.Location;
import android.os.Bundle;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.model.LatLng;
import com.webgeoservices.woosmapgeofencing.Woosmap;

import com.webgeoservices.woosmapgeofencing.WoosmapSettings;
import com.webgeoservices.woosmapgeofencingcore.database.*;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;

    private Woosmap woosmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Instanciate woosmap object
        this.woosmap = Woosmap.getInstance().initializeWoosmap(this);

        this.woosmap.setRegionLogReadyListener( new WoosRegionLogReadyListener() );
        this.woosmap.setLocationReadyListener( new WoosLocationReadyListener() );

        // Define a Woosmap project key for retrieving assets previously uploaded in the Woosmap platform
        WoosmapSettings.privateKeyWoosmapAPI = "woosmapPrivateKey";

        // Live tracking - High accuracy - high power battery usage - permanent notification in background
        this.woosmap.startTracking(Woosmap.ConfigurationProfile.passiveTracking);
    }

    public class WoosRegionLogReadyListener implements Woosmap.RegionLogReadyListener {

        // The RegionLogReadyCallback callback is triggered when the user enters (or exits) in a geofence
        public void RegionLogReadyCallback(RegionLog regionLog) {
            // use regionLog.didEnter value to know if the user enters (didEnter=true) or exits (didEnter=false) the geofence
            Log.d("WoosmapGeofencing", "eventName:"+ regionLog.eventName + " - regionLog: " + regionLog.id);

            // for retrieving additional asset data, a get request to the internal device DB is required
            POI poi = WoosmapDb.getInstance(getApplicationContext()).getPOIsDAO().getPOIbyStoreId(regionLog.idStore);
            Log.d("WoosmapGeofencing", "eventName:" + regionLog.eventName + " - POI name:" + poi.name);
        }
    }

    public class WoosLocationReadyListener implements Woosmap.LocationReadyListener {

        public void LocationReadyCallback(Location location) {
            Log.d("WoosmapGeofencing", "User location collected - lat: " + location.getLatitude() + ", lng: " + location.getLongitude());

        }
    }

    @Override
    public void onStart() { super.onStart(); }

    @Override
    public void onResume() { super.onResume(); woosmap.onResume(); }

    @Override
    public void onPause() { super.onPause(); woosmap.onPause();}

    @Override
    protected void onDestroy() { woosmap.onDestroy(); super.onDestroy(); };
}

    
Was this article helpful?
Have more questions? Submit a request