SDK Methods

Listeners

Now that the location tracking is set up, you can subscribe to locations and events and use the data locally on your device or send it directly to your own backend server.

To do that, you need to set the location and event listener to true using the below method. By default the status will set to false and needs to be set to true in order to stream the location and events updates to the same device or other devices.

GeoSpark.toggleListener(locations, events, new GeoSparkCallback() {
@Override
public void onSuccess(GeoSparkUser geosparkUser) {
// do something when toggle listener success
// access locations listener status with geosparkUser.getLocationListenerStatus()
// access events listener status with geosparkUser.getEventListenerStatus()
}
@Override
public void onFailure(GeoSparkError geosparkError ) {
// do something when toggle listener failure
}
});

You can also get the current status of listeners with the below method.

GeoSpark.getListenerStatus(new GeoSparkCallback() {
@Override
public void onSuccess(GeoSparkUser geosparkUser) {
// do something when get listener status success
// get location listener status with geosparkUser.getLocationListenerStatus
// get events listener status with geosparkUser.getEventListenerStatus
}
@Override
public void onFailure(GeoSparkError geosparkError ) {
// do something when get listener status failure
}
});

Subscribe Location, Trip Status and Events

Now that we have enabled the location listener, use the below method to subscribe to your own or other user's location updates and events.

//subscribe to own location updates
GeoSpark.subscribeLocation();
//subscribe to other user's location updates
GeoSpark.subscribeUserLocation("GeoSpark User ID");
//subscribe to event updates
GeoSpark.subscribeEvents();
//subscribe to trip status
GeoSpark.subscribeTripStatus("GeoSpark Trip ID");

To listen to location updates create a class that extends GeoSparkReceiver. Then register the receiver by adding a receiver element to the application element in your manifest.

<application>
...
<receiver android:name=".MyGeoSparkReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.geospark.android.RECEIVED"/>
</intent-filter>
</receiver>
...
</application>

Then add the code to the receiver.

public class MyGeoSparkReceiver extends GeoSparkReceiver {
@Override
public void onLocationUpdated(Context context, GeoSparkLocation geosparkLocation) {
// receive own location updates here
// do something with location data using location
}
@Override
public void onLocationReceived(Context context, GeoSparkLocationReceived geosparkLocationReceived) {
// receive other user's location updates here
// do something with location
}
@Override
public void onEventReceived(Context context, GeoSparkEvent geoSparkEvent) {
//access event data here
}
@Override
public void onReceiveTripStatus(Context context, TripStatusListener listener) {
// receive real time trip status here
// do something with trip status data
}
@Override
public void onError(Context context, GeoSparkError geosparkError) {
//access error data here
}
}

To stop receiving location updates, use the below method.

//unsubscribe to own location updates
GeoSpark.unSubscribeLocation();
//unsubscribe to other user's location updates
GeoSpark.unSubscribeUserLocation("GeoSpark User ID");
//unsubscribe to other event updates
GeoSpark.unSubscribeEvents();
//unsubscribe to trip status
GeoSpark.unSubscribeTripStatus("GeoSpark Trip ID");

You should set the location and event listener to false if you do not need to stream the user location.

Toggle Event Flags

To listen to events on the server-side, you should enable events for the user using the method below.

GeoSpark.toggleEvents(geofence, trip, location, movingGeofence, new GeoSparkCallback() {
@Override
public void onSuccess(GeoSparkUser geosparkUser) {
// do something when toggle events success
// access location events status with geosparkUser.getLocationEvents()
// access geofence events status with geosparkUser.getGeofenceEvents()
// access trips events status with geosparkUser.getTripsEvents()
// get moving geofence event status with geosparkUser.getMovingGeofenceEvents()
}
@Override
public void onFailure(GeoSparkError error ) {
// do something when toggle events failure
}
});

Trips

Use the below code to create a trip directly from the SDK. Set Boolean value true to create offline trip and false to create online trip.

GeoSpark.createTrip(null, null, Boolean, new GeoSparkCreateTripCallback() {
@Override
public void onSuccess(GeoSparkCreateTrip geoSparkCreateTrip) {
// do something when create trip success
// access geospark trip created timestamp with geoSparkCreateTrip.getCreatedAt()
// access geospark trip user id with geoSparkCreateTrip.getUserId()
// access geospark trip id with geoSparkCreateTrip.getTripId()
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when create trip error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

To get the trip details.

GeoSpark.getTripDetails("GEOSPARK-TRIP-ID", new GeoSparkTripDetailCallback() {
@Override
public void onSuccess(GeoSparkTripDetail geoSparkTripDetail) {
// do something when get trip details success
// access geospark trip created timestamp with geoSparkTripDetail.getCreatedAt()
// access geospark trip user id with geoSparkTripDetail.getUserId()
// access geospark trip id with geoSparkTripDetail.getTripId()
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when get trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

To get the trip status.

GeoSpark.getTripStatus("GEOSPARK-TRIP-ID", new GeoSparkTripStatusCallback() {
           @Override
           public void onSuccess(GeoSparkTripStatus geosparkTrip) {
               // do something when get trip details success
      // access geospark trip distance with geosparkTrip.getDistance()
      // access geospark trip speed with geosparkTrip.getSpeed()
           }
           @Override
           public void onFailure(GeoSparkError geosparkError) {
               // do something when get trip details error
      // access geospark error code with geosparkError.getCode()
      // access geospark error message with geosparkError.getMessage()
           }
});

To get the trip summary.

GeoSpark.getTripSummary("GEOSPARK-TRIP-ID", new GeoSparkTripSummaryCallback() {
@Override
public void onSuccess(GeoSparkTripSummary geoSparkTripSummary) {
// do something when get trip details success
// access geospark trip distance covered with geoSparkTripSummary.getDistanceCovered()
// access geospark trip route with geoSparkTripSummary.getRoute()
// access geospark trip duration with geoSparkTripSummary.getDuration()
// access geospark trip id with geoSparkTripSummary.getTripId()
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when get trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

To get the active trips. Set Boolean value true to get offline trips and false to get online trips.

GeoSpark.activeTrips(Boolean, new GeoSparkActiveTripsCallback() {
@Override
public void onSuccess(GeoSparkTrip geoSparkTrip) {
geoSparkTrip.getActiveTrips();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getCode();
geoSparkError.getMessage();
}
});

Use the below code to start the trip with the previously created trip id.

GeoSpark.startTrip("GEOSPARK-TRIP-ID", "GEOSPARK-TRIP-DESCRIPTION", new GeoSparkTripCallback() {
@Override
public void onSuccess(String message) {
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when start trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

Use the below code to pause the trip with the previously started trip id.

GeoSpark.pauseTrip("GEOSPARK-TRIP-ID", new GeoSparkTripCallback() {
@Override
public void onSuccess(String message) {
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when start trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

To resume the trip.

GeoSpark.resumeTrip("GEOSPARK-TRIP-ID", new GeoSparkTripCallback() {
@Override
public void onSuccess(String message) {
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when start trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});

To stop the trip.

GeoSpark.stopTrip("GEOSPARK-TRIP-ID", new GeoSparkTripCallback() {
@Override
public void onSuccess(String message) {
}
@Override
public void onFailure(GeoSparkError geosparkError) {
// do something when start trip details error
// access geospark error code with geosparkError.getCode()
// access geospark error message with geosparkError.getMessage()
}
});