Commit 0856e7d9 authored by Chris Scott's avatar Chris Scott

Merge branch geofencing

parents e64e5dae 8f24e6c5
......@@ -50,6 +50,7 @@
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<script type="text/javascript" src="js/LongPress.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
/**
* Custom google-maps @event LongClick hack.
*/
function LongPress(map, maxTime) {
this.maxTime = maxTime;
this.isDragging = false;
var me = this;
me.map = map;
google.maps.event.addListener(map, 'mousedown', function(e) {
me.onMouseDown_(e);
});
google.maps.event.addListener(map, 'mouseup', function(e) {
me.onMouseUp_(e);
});
google.maps.event.addListener(map, 'drag', function(e) {
me.onMapDrag_(e);
});
}
LongPress.prototype.onMouseUp_ = function(e) {
var now = +new Date;
if (now - this.downTime > this.maxTime && this.isDragging === false) {
google.maps.event.trigger(this.map, 'longpress', e);
}
}
LongPress.prototype.onMouseDown_ = function() {
this.downTime = +new Date;
this.isDragging = false;
}
LongPress.prototype.onMapDrag_ = function(e) {
this.isDragging = true;
};
......@@ -54,6 +54,10 @@ var app = {
*/
currentLocation: null,
/**
* @property geofence {google.maps.Cirlce}
*/
geofence: undefined,
/**
* @private
*/
btnEnabled: undefined,
......@@ -83,6 +87,9 @@ var app = {
canvas.width(window.clientWidth);
app.map = new google.maps.Map(canvas[0], mapOptions);
// Add custom LongPress event to google map so we can add Geofences with longpress event!
new LongPress(app.map, 500);
},
// Bind Event Listeners
//
......@@ -188,7 +195,7 @@ var app = {
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
desiredAccuracy: 0,
stationaryRadius: 50,
distanceFilter: 50,
distanceFilter: 25,
disableElasticity: false, // <-- [iOS] Default is 'false'. Set true to disable speed-based distanceFilter elasticity
locationUpdateInterval: 5000,
minimumActivityRecognitionConfidence: 80, // 0-100%. Minimum activity-confidence for a state-change
......@@ -214,6 +221,33 @@ var app = {
}
});
bgGeo.onGeofence(function(identifier) {
alert('Enter Geofence: ' + identifier);
console.log('[js] Geofence ENTER: ', identifier);
});
// Add longpress event for adding GeoFence of hard-coded radius 200m.
google.maps.event.addListener(app.map, 'longpress', function(e) {
if (app.geofence) {
app.geofence.setMap(null);
}
bgGeo.addGeofence({
identifier: 'MyGeofence',
radius: 200,
latitude: e.latLng.lat(),
longitude: e.latLng.lng()
}, function() {
app.geofence = new google.maps.Circle({
fillColor: '#00cc00',
fillOpacity: 0.4,
strokeOpacity: 0,
radius: 200,
center: e.latLng,
map: app.map
});
})
});
// Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app.
var settings = ENV.settings;
......
......@@ -10,6 +10,7 @@
@interface CDVBackgroundGeolocation : CDVPlugin
@property (nonatomic, strong) NSString* syncCallbackId;
@property (nonatomic, strong) NSString* geofenceCallbackId;
@property (nonatomic, strong) NSMutableArray* stationaryRegionListeners;
- (void) configure:(CDVInvokedUrlCommand*)command;
......@@ -24,5 +25,7 @@
- (void) sync:(CDVInvokedUrlCommand *)command;
- (void) getOdometer:(CDVInvokedUrlCommand *)command;
- (void) resetOdometer:(CDVInvokedUrlCommand *)command;
- (void) addGeofence:(CDVInvokedUrlCommand *)command;
- (void) onGeofence:(CDVInvokedUrlCommand *)command;
@end
......@@ -10,7 +10,7 @@
NSDictionary *config;
}
@synthesize syncCallbackId, stationaryRegionListeners;
@synthesize syncCallbackId, geofenceCallbackId, stationaryRegionListeners;
- (void)pluginInitialize
{
......@@ -18,6 +18,7 @@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLocationChanged:) name:@"TSLocationManager.location" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStationaryLocation:) name:@"TSLocationManager.stationary" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterGeofence:) name:@"TSLocationManager.geofence" object:nil];
}
/**
......@@ -114,6 +115,17 @@
[bgGeo stopBackgroundTask];
}
- (void) onEnterGeofence:(NSNotification*)notification
{
if (self.geofenceCallbackId == nil) {
return;
}
CLCircularRegion *region = notification.object;
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:region.identifier];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:self.geofenceCallbackId];
}
/**
* Fetches current stationaryLocation
*/
......@@ -160,6 +172,23 @@
[self.stationaryRegionListeners addObject:command.callbackId];
}
- (void) addGeofence:(CDVInvokedUrlCommand*)command
{
NSDictionary *cfg = [command.arguments objectAtIndex:0];
[bgGeo addGeofence:[cfg objectForKey:@"identifier"]
radius:[[cfg objectForKey:@"radius"] doubleValue]
latitude:[[cfg objectForKey:@"latitude"] doubleValue]
longitude:[[cfg objectForKey:@"longitude"] doubleValue]
];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) onGeofence:(CDVInvokedUrlCommand*)command
{
self.geofenceCallbackId = command.callbackId;
}
/**
* Called by js to signify the end of a background-geolocation event
*/
......
......@@ -4,6 +4,8 @@
@interface TSLocationManager : NSObject <CLLocationManagerDelegate>
@property (nonatomic) CLLocationDistance odometer;
- (void) configure:(NSDictionary*)config;
- (void) start;
- (void) stop;
......@@ -19,5 +21,7 @@
- (void) onAppTerminate;
- (BOOL) isEnabled;
- (NSDictionary*) locationToDictionary:(CLLocation*)location;
- (void) addGeofence:(NSString*)identifier radius:(CLLocationDistance)radius latitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude;
@end
......@@ -6,7 +6,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
uXAE4LxGF9ekkpUCvnRgTZf4wvM=
93eNJDqyN5vuBJn9oYeT7lb2x1w=
</data>
<key>Info.plist</key>
<data>
......@@ -21,7 +21,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
uXAE4LxGF9ekkpUCvnRgTZf4wvM=
93eNJDqyN5vuBJn9oYeT7lb2x1w=
</data>
<key>Modules/module.modulemap</key>
<data>
......
......@@ -156,6 +156,36 @@ module.exports = {
'resetOdometer',
[]);
},
/**
* add geofence
*/
addGeofence: function(config, success, failure) {
config = config || {};
if (!config.identifier) {
throw "#addGeofence requires an 'identifier'";
}
if (!(config.latitude && config.longitude)) {
throw "#addGeofence requires a #latitude and #longitude";
}
if (!config.radius) {
throw "#addGeofence requires a #radius";
}
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'addGeofence',
[config]);
},
onGeofence: function(success, failure) {
if (!typeof(success) === 'function') {
throw "#onGeofence requires a success callback";
}
exec(success,
failure || function() {},
'BackgroundGeoLocation',
'onGeofence',
[]);
},
_setTimestamp: function(rs) {
// Transform timestamp to Date instance.
if (typeof(rs) === 'object') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment