Unlimited Geofences and Location Monitoring for Ionic Apps
So, you guys probably know that natively, Android limits geofences to 100 locations while iOS limits it to a mere 20. This can be a struggle when you want to monitor hundreds of geofences. I know there are ways to remove old geofences and recreate another 100/20 based on the user’s latest geofence but that can be draining on the phone’s resources, thus draining on the battery.
This is where Radar.io comes in. With the free plan, you are able to have up to 1,000 geofences and unlimited with their paid plans.
In order to use Radar’s native functionality for Android and iOS in Ionic application, I created a Cordova plugin. This makes it that much easier to monitor user location.
The plugin can be downloaded from my Github:
Install the plugin
ionic cordova plugin add ionic-radar
Usage
- Setting up Radar Keys
For Android, under src -> android -> com -> Radar
in Radar.java
, add your Radar Key in the initialize method
public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); Context context =
this.cordova.getActivity().getApplicationContext(); Radar.initialize(context, "your_radar_key"); Log.d(TAG, "Initializing Radar Plugin");}
For iOS, under ios
in RadarWrapper.swift
, add your Radar Key in the pluginInitialize function
override func pluginInitialize() { Radar.initialize(publishableKey: "your_radar_key");}
- Declaring Cordova
On whichever page you want to use the Radar plugin, you can declare cordova
just below your exports, as follows:
declare var cordova: any;
- Start Tracking
When you want to begin tracking the user’s location in the background, simply call the start tracking method:
cordova.plugins.RadarPlugin.startTracking(value => {});
- Stop Tracking
To stop background location tracking, run:
cordova.plugins.RadarPlugin.stopTracking(value => {});
- Set User ID
If you want to give users a customized user ID rather than the one generated by Radar, you can do it by running the following:
cordova.plugins.RadarPlugin.stopTracking(userID, value => {});
- Check Permission
Check if the app has location permission:
cordova.plugins.RadarPlugin.checkPermissions();
- Get Permission
Ask the user for location permission:
cordova.plugins.RadarPlugin.getPermissions();
I haven’t yet integrated all of the methods from the native Radar SDK and will get around to doing it sometime! Feel free to contribute to the plugin on Github.
If you guys have any question about the plugin and how it works, feel free to leave a comment here, post an issue on Github, or email me at clipitapplication@gmail.com and I will gladly help.