Skip to content

Quickstart

Ekambaram edited this page Jan 7, 2021 · 14 revisions

The GeoSpark React Native SDK makes it quick and easy to build a location tracker for your React Native app. We provide powerful and customizable tracking modes and features that can be used to collect your users location updates.

Here’s a link to our example app : https://github.com/geosparks/geospark-react-native-example

Install the module

In your project directory, install from npm, and then link it.

$ npm install react-native-geospark --save
$ react-native link react-native-geospark

Installation

iOS

Install using Cocoapods, open podfile and add SDK to file.

pod 'GeoSpark'

Once you have updated your podfile,****run pod install in your terminal.

Configure project

To configure the location services, add following entries to the Info.plist file.

Then, in your project settings, go to Capabilities > Background Modes and turn on background fetch, location updates, remote-notifications.

Then, go to Build Settings in the project targets and change 'Always Embed Swift Standard Libraries' to 'Yes'.

Manual Linking

  1. Open the iOS module files, located inside node_modules/react-native-geospark/ios/.

  2. Open the app workspace file (AppName.xcworkspace) in Xcode.

  3. Move the RNGeoSpark.h and RNGeoSpark.m files to your project. When shown a popup window, select Create groups.

Android

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

dependencies {
 implementation 'com.geospark.android:geospark:3.1.0'
}

Initialize SDK

Import the module in App.js file

import GeoSpark from 'react-native-geospark'; 

Android

Initialize the SDK with your publishable key.

//In onCreate method of your Application class include the code below.
public class MainApplication extends Application implements ReactApplication {
  @Override   
  public void onCreate() {        
    super.onCreate();        
    SoLoader.init(this, /* native exopackage */ false);     
    GeoSpark.initialize(this, "PUBLISH_KEY");   
  }
};    

iOS

Import GeoSpark into your AppDelegate file.

Objective-C

#import <GeoSpark/GeoSpark.h>

Initialize the SDK in your AppDelegate class before calling any other GeoSpark methods under this application:didFinishLaunchingWithOptions:

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
   [GeoSpark intialize:@"PUBLISHABLEKEY" :nil :nil :nil :nil :AWSRegionUnknown];  
    return YES;
}

Creating Users

Once the SDK is initialized, we need to create or get a user to start the tracking and use other methods. Every user created will have a unique GeoSpark identifier which will be used later to login and access developer APIs. We can call it as GeoSpark userId.

GeoSpark.createUser("Description", success => {
 // do something on success    
},
error => {
// do something on error
});

The option user description 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 later using the below code.

GeoSpark.setDescription("SET USER DESCRIPTION HERE");

If you already have a GeoSpark userID which you would like to reuse instead of creating a new user, use the below to get user session.

GeoSpark.getUser("USER-ID", success => {
// do something on success    
},
error => {
// do something on error
});

Request Permissions

Get location permission from the App user on the device. Also check if the user has turned on location services for the device. In addition, get motion permission for iOS.

// Call this method to check Location Permission for Android & iOS
GeoSpark.checkLocationPermission( status => {    
// do something with status
});

​// Call this method to request Location Permission for Android & iOS
GeoSpark.requestLocationPermission();

Android

// Call this method to check Location services for Android
GeoSpark.checkLocationServices( status => {    
// do something with status
}); 
​// Call this method to request Location services for Android
GeoSpark.requestLocationServices();

To start tracking the location above Android 10

// Call this method to check background location permission for Android
GeoSpark.checkBackgroundLocationPermission( status => {        
// do something with status
});
// Call this method to request background location Permission for Android
GeoSpark.requestBackgroundLocationPermission();

Location Tracking

Start Tracking

GeoSpark.startTracking(TrackingMode);

Use the tracking modes while you use the startTracking method GeoSpark.startTracking

Tracking Modes

GeoSpark 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 is the battery consumption. Android you must use foreground service for continuous tracking.

Mode Battery usage **Updates every ** **Optimised for/advised for **
Active 6% - 12% 25 ~ 250 meters Ride Hailing / Sharing
Reactive 3% - 6% 50 ~ 500 meters On Demand Services
Passive 0% - 1% 100 ~ 1000 meters Social Apps
//active tracking
GeoSpark.startTracking(GeoSpark.TrackingMode.ACTIVE);
// reactive tracking
GeoSpark.startTracking(GeoSpark.TrackingMode.REACTIVE);
// active tracking
GeoSpark.startTracking(GeoSpark.TrackingMode.ACTIVE);

Custom Tracking Modes

The SDK also allows you define a custom tracking mode that allows you to customize and build your own tracking modes.

Type Unit Unit Range
Distance Interval Meters 1m ~ 2500m
Time Interval Seconds 10s ~ 10800s

Android

Distance between location updates example code:

//Update location based on distance between locations.
GeoSpark.startTrackingDistanceInterval("DISTANCE IN METERS", "STATIONARY DURATION IN SECONDS",
                        GeoSpark.DesiredAccuracy.HIGH);

Time between location updates example code:

//Update location based on time interval.
GeoSpark.startTrackingTimeInterval("INTERVAL IN SECONDS", GeoSpark.DesiredAccuracy.HIGH);

iOS

GeoSpark.startTrackingCustom(allowBackground,pauseAutomatic,activityType,
                              desiredAccuracy,showBackIndicator,distanceFilter,accuracyFilter);

Example

GeoSpark.startTrackingCustom(true,true,GeoSpark.ActivityType.FITNESS,
          GeoSpark.DesiredAccuracyIOS.BEST,true,10,10);

You may see a delay if the user's device is in low power mode or has connectivity issues.

Stop Tracking

To stop the tracking use the below method.

GeoSpark.stopTracking();

Clone this wiki locally