Explore how to publish and subscribe locations for Android.
Creating Users
Once our SDK is initialized, we need to create or get a user to start the tracking and use other methods. Each user created will have a unique Roam identifier which will be used later to login and access developer APIs. You can call it Roam userId.
Adding metadata to users is optional and you can pass null while creating users to ignore that field.
// create user with meta-data and description// Declare meta-data as json// Description as stringval metadata =JSONObject() metadata.put("key", "value") Roam.createUser("SET-USER-DESCRIPTION-HERE", metadata, object : RoamCallback {overridefunonSuccess(roamUser: RoamUser) {// Access Roam user data below// roamUser.getUserId();// roamUser.getDescription();// roamUser.getEventListenerStatus();// roamUser.getLocationListenerStatus();// roamUser.getLocationEvents();// roamUser.getGeofenceEvents();// roamUser.getMovingGeofenceEvents();// roamUser.getTripsEvents(); }overridefunonFailure(roamError: RoamError) {// Access the error code & message below// roamError.getCode()// roamError.getMessage() }})
// update user with meta-data and description// Declare meta-data as dictionary// Description as stringJSONObject metadata =newJSONObject(); metadata.put("key","value"); Roam.createUser("YOUR-USER-DESCRIPTION-GOES-HERE", metadata,newRoamCallback() { @OverridepublicvoidonSuccess(RoamUser roamUser) {// Access Roam user data below// roamUser.getUserId();// roamUser.getDescription();// roamUser.getEventListenerStatus();// roamUser.getLocationListenerStatus();// roamUser.getLocationEvents();// roamUser.getGeofenceEvents();// roamUser.getMovingGeofenceEvents();// roamUser.getTripsEvents(); } @OverridepublicvoidonFailure(RoamError roamError) {// Access error code & message below// roamError.getCode();// roamError.getMessage(); } });
The "user description" option can be used to update your user information such as name, address or add an existing user ID. Make sure the information is encrypted if you are planning to save personal user information like email or phone number.
You can always set or update user descriptions and meta-data later using the code below.
// update user with meta-data and description// Declare meta-data as dictionary// Description as stringval metadata =JSONObject()metadata.put("key","value") Roam.setDescription("SET-USER-DESCRIPTION-HERE", metadata)
// update user with meta-data and description// Declare meta-data as dictionary// Description as stringJSONObject metadata =newJSONObject(); metadata.put("key","value"); Roam.setDescription("SET-USER-DESCRIPTION-HERE", metadata);
Get User
If you already have a Roam userID that you would like to reuse instead of creating a new user, use the below to get the user session.
Specify the tracking mode while you use the startTracking method Roam.startTracking
Tracking Modes
Roam has three default tracking modes along with a custom version. They differ based on the frequency of location updates and battery consumption. The higher the frequency, the higher the battery consumption. You must use foreground service for continuous tracking.
Our SDK also provides a custom tracking mode that allows you to customize and build your own tracking modes.
Distance between location updates example code:
// Define a custom tracking method with desired distance interval, stop duration and accuracyval trackingMode = RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>, <STOP-INTERVAL-IN-SECONDS>) .setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH) .build()// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode, object:TrackingCallback(){overridefunonSuccess(s: String?) {//do something }overridefunonError(roamError: RoamError?) {//do something } })
// Define a custom tracking method with desired distance interval, stop duration and accuracyRoamTrackingMode trackingMode =new RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>,<STOP-INTERVAL-IN-SECONDS>).setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH).build();// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode,newTrackingCallback() { @OverridepublicvoidonSuccess(String s) {//do something } @OverridepublicvoidonError(RoamError roamError) {//do something } });
Time between location updates example code:
// Define a custom tracking method with desired time interval and accuracyval trackingMode = RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>) .setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH) .build()// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode, object:TrackingCallback(){overridefunonSuccess(s: String?) {//do something }overridefunonError(roamError: RoamError?) {//do something } })
// Define a custom tracking method with desired time interval and accuracyRoamTrackingMode trackingMode =new RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>).setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH).build();// Start the tracking with the above created custom tracking methodRoam.startTracking(trackingMode,newTrackingCallback() { @OverridepublicvoidonSuccess(String s) {//do something } @OverridepublicvoidonError(RoamError roamError) {//do something } });
You may see a delay if the user's device is in low power mode or has connectivity issues.
You can pass an empty user_id which is an optional field from v0.0.14 to unsubscribe from all the users.
Location Listener
Before adding listeners for locations and other data, we need to toggle the listener flags for the user in order to get the data.
To do that, you need to set the location and event listener to true using the below method. By default, the status will be set to false and needs to be set to true in order to stream the location and event updates to the same device or other devices.
Roam.toggleListener(locations, events, object : RoamCallback {overridefunonSuccess(roamUser: RoamUser) {// do something when toggle listener success// access location listener status with roamUser.getLocationListenerStatus()// access event listener status with roamUser.getEventListenerStatus() }overridefunonFailure(roamError: RoamError) {// do something when toggle listener failure }})
You can also get the current status of listeners with the below method.
Roam.getListenerStatus(object : RoamCallback {overridefunonSuccess(roamUser: RoamUser) {// do something when get listener status success// get location listener status with roamUser.getLocationListenerStatus// get event listener status with roamUser.getEventListenerStatus }overridefunonFailure(roamError: RoamError) {// do something when get listener status failure }})
To listen to events on the server-side, you should enable events for the user using the method below.
Roam.toggleEvents(geofence, trip, location, movingGeofence, object : RoamCallback {overridefunonSuccess(roamUser: RoamUser) {// do something when toggle events success// access location event status with roamUser.getLocationEvents()// access geofence event status with roamUser.getGeofenceEvents()// access trip events status with roamUser.getTripsEvents()// get moving geofence event status with roamUser.getMovingGeofenceEvents() }overridefunonFailure(error: roamError) {// do something when toggle events failure }
Now, to listen to location updates and events, create a class that extends RoamReceiver. Then register the receiver by adding a receiver element to the application element in your manifest.
publicclassLocationReceiver : RoamReceiver() {overridefunonLocationUpdated(context: Context, roamLocations: MutableList<RoamLocation>) {// receive own location updates here// do something with location data using location }overridefunonLocationReceived(context: Context, roamLocationReceived: RoamLocationReceived) {// receive other user's location updates here// do something with location }overridefunonEventReceived(context: Context, roamEvent: RoamEvent) {//access event data here }overridefunonReceiveTripStatus(context: Context, listener:List<TripStatusListener>) {// receive real time trip status here// do something with trip status data }overridefunonError(context: Context, roamError: RoamError) {//access error data here }}
publicclassLocationReceiverextendsRoamReceiver { @OverridepublicvoidonLocationUpdated(Context context,List<RoamLocation> roamLocations) {// receive own location updates here// do something with location data using location } @OverridepublicvoidonLocationReceived(Context context,RoamLocationReceived roamLocationReceived) {// receive other user's location updates here// do something with location } @OverridepublicvoidonEventReceived(Context context,RoamEvent roamEvent) {//access event data here } @OverridepublicvoidonReceiveTripStatus(Context context,List<TripStatusListener> listener) {// receive real time trip status here// do something with trip status data } @OverridepublicvoidonError(Context context,RoamError roamError) {//access error data here }}