Sign up here to create an account with GeoSpark. Post signup, you can create your first project from the dashboard screen.
Once you have created your project, you can add App Name, Package Name and create Android applications. You can get the publishable key from your project settings page which has to be used while integrating the SDK with your application.
In onCreate method of your Application class, Initialize the SDK with your PublishableKey.
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
GeoSpark.initialize(this, "PUBLISHABLE_KEY");
}
}
Step 4: Create GetStartedActivity.java
Create getstartedactivity.xml and add a TextView.
Now create a GetStartedActivity.java and add GeoSpark createUser() method inside your onClickListener of the Button, to create a user which returns UserID. The SDK needs a UserID object to identify the device.
To enable location, call requestLocationPermissions and requestLocationServices method. For Android 6.0 and above, calling this method will trigger a location permission popup that the user has to allow and startTracking the user location.
Add Google Map SDK in MainActivity.java. Add Marker Image in centre of Google Map in main_activity.xml to get location co-ordinates.
@Override
public void onCameraIdle() {
if (mMap.getCameraPosition().target.latitude != 0.0 && mMap.getCameraPosition().target.longitude != 0.0) {
mMap.getCameraPosition().target.latitude//Get latitude from center of map
mMap.getCameraPosition().target.longitude//Get longitude from center of map
}
}
Use GeoSpark's createGeofence, geofenceList and deleteGeofence method to manage geofence data like (Latitude, Longitude, ExpireTime and Radius) in GeoSpark server.
Create Geofence
Make sure radius should be (50 -1000 meters) and set expiry time less than 24hrs.
GeoSpark.createGeofence(this, latitude, longitude, radius, expireInSeconds,
new GeoSparkGeofenceCallBack() {
@Override
public void onSuccess(GeoSparkGeofence geoSparkGeofence) {
geoSparkGeofence.getId();
geoSparkGeofence.getCreatedAt();
geoSparkGeofence.getExpiresAt();
geoSparkGeofence.getCoordinates();
geoSparkGeofence.getRadius();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});
Geofence List
GeoSpark.geofenceList(this, new GeoSparkGeofenceCallBack() {
@Override
public void onSuccess(GeoSparkGeofence geoSparkGeofence) {
geoSparkGeofence.getGeofenceList();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});
Delete Geofence
GeoSpark.deleteGeofence(this, "GeofenceId", new GeoSparkGeofenceCallBack(){
@Override
public void onSuccess(GeoSparkGeofence geoSparkGeofence) {
geoSparkGeofence.getId();
geoSparkGeofence.getMessage();
}
@Override
public void onFailure(GeoSparkError geoSparkError) {
geoSparkError.getErrorCode();
geoSparkError.getErrorMessage();
}
});
Step 6: Add Notification Message
Add a Local Notification message to notify when user reaches the geofence area.
Step 7: Trigger Notification
Get the user location from GeoSparkReceiver and compare it with the already created geofence Location. If user location is inside geofence radius, trigger a local Notification.
public class MyGeoSparkReceiver extends GeoSparkReceiver {
@Override
public void onLocationUpdated(Context context, Location location,
GeoSparkUser geoSparkUser, String activity){
// Compare created geofence location with user location to trigger // notification.
}
}
Now your application for location triggered notifications is successfully built.
Ready to take the next steps?
Thank you for reading! If you're interested in seeing our product in action, click here to get in contact with our team. Or, if you're ready to get started, you can sign up here to start using our SDK and APIs today.