Integrating Location Tracking using GeoSpark Android SDK

Ravikanth
Ravikanth
Senior Software Engineer
February 8, 2019

Overview

This article covers the integration of GeoSpark Android SDK. GeoSpark Android SDK enables you to track your user location in both online and offline states. The SDK enables you to track location in foreground, background and terminated app state. Our SDK tracks location with minimal battery drain.

Here are the steps involved in integrating GeoSpark's Android SDK with your app:

https://youtu.be/qwWxcqQpfwY

Step 1: Install SDK

Open Android Studio, start a new Android Studio project and create project.

Select minimum SDK Android 4.1.

Select Empty Activity and finish.

Install the SDK to your project via Gradle in Android Studio and add the dependencies below in your app build.gradle file.

android { defaultConfig{ targetSdkVersion 28 } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'com.geospark.android:geospark:1.5'

Step 2: Set DeviceToken

Set FCM DeviceToken to create a user. If you haven't set FCM DeviceToken already, then add Firebase to your Android project.

GeoSpark.setDeviceToken(this, "FCM DeviceToken");

Step 3: Initialize SDK

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 PUBLISH _KEY.

public void onCreate() { super.onCreate(); GeoSpark.initialize(this, "PUBLISH_KEY"); }

Step 4: Create User

GeoSpark SDK needs a User ID object to identify the device. The SDK has a convenience method createUser() to create a user which returns User ID.

GeoSpark.createUser(this,"User Description", new GeoSparkCallBack() { @Override public void onSuccess(GeoSparkUser geoSparkUser) { //geoSparkUser.getUserId(); } @Override public void onFailure(GeoSparkError geoSparkError) { //geoSparkError.getErrorCode(); //geoSparkError.getErrorMessage(); } });

Step 5: Start Location Tracking

To enable location, call the requestPermissions 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.

if(!GeoSpark.checkLocationPermission(this)) { GeoSpark.requestLocationPermission(this); } else if (!GeoSpark.checkLocationServices(this)) { GeoSpark.requestLocationServices(this); } else{ GeoSpark.startTracking(this); }

Start viewing your user's location data on the dashboard.

Your Application is successfully integrated with GeoSpark Android SDK.

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.

Unlock Location Technology

Technology
Ravikanth
Ravikanth
Senior Software Engineer
February 8, 2019