Skip to content

Instantly share code, notes, and snippets.

@stevenou
Created February 14, 2012 21:15
Show Gist options
  • Save stevenou/1830512 to your computer and use it in GitHub Desktop.
Save stevenou/1830512 to your computer and use it in GitHub Desktop.
Geofencing in Titanium
// you probably want to keep track of the backgrounding state of the app to respond differently
var backgrounded = false;
Ti.App.addEventListener('pause', function() {
backgrounded = true;
});
Ti.App.addEventListener('resume', function() {
backgrounded = false;
});
Ti.App.addEventListener('resumed', function() {
backgrounded = false;
});
// start monitoring a region
Ti.Geolocation.startMonitoring({
lat: 123.33,
lng: 45.34,
accuracy: 5,
radius: 100,
identifier: "the region identifier"
});
// react when a region is entered
Ti.Geolocation.addEventListener('enteredRegion', function(e) {
if (backgrounded) {
// use a local notification
} else {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'Geofencing',
message: "You entered the region identified by " + e.region.identifier
});
alertDialog.show();
}
});
// react when a region is exited
Ti.Geolocation.addEventListener('exitedRegion', function(e) {
if (backgrounded) {
// use a local notification
} else {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'Geofencing',
message: "You exited the region identified by " + e.region.identifier
});
alertDialog.show();
}
});
// stop monitoring a region
Ti.Geolocation.stopMonitoring({
lat: 123.33,
lng: 45.34,
accuracy: 5,
radius: 100,
identifier: "the region identifier"
});
// get an array of monitored regions - each region is a hash of lat, lng, accuracy, radius, identifier
regions = Ti.Geolocation.monitoredRegions();
// this file located in /Library/Application Support/Titanium/mobilesdk/osx/{version_number}
// add the following methods to GeolocationModule.mm
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:NUMBOOL(YES),@"success",[NSDictionary dictionaryWithObjectsAndKeys:NUMDOUBLE(region.center.latitude),@"lat",NUMDOUBLE(region.center.longitude),@"lng",NUMDOUBLE(region.radius),@"radius",region.identifier,@"identifier",nil],@"region",nil];
if ([self _hasListeners:@"enteredRegion"])
{
[self fireEvent:@"enteredRegion" withObject:event];
}
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:NUMBOOL(YES),@"success",[NSDictionary dictionaryWithObjectsAndKeys:NUMDOUBLE(region.center.latitude),@"lat",NUMDOUBLE(region.center.longitude),@"lng",NUMDOUBLE(region.radius),@"radius",region.identifier,@"identifier",nil],@"region",nil];
if ([self _hasListeners:@"exitedRegion"])
{
[self fireEvent:@"exitedRegion" withObject:event];
}
}
-(void)startMonitoring:(id)args
{
ENSURE_UI_THREAD(startMonitoring,args);
ENSURE_SINGLE_ARG(args,NSDictionary);
CGFloat lat = [TiUtils floatValue:[args objectForKey:@"lat"]];
CGFloat lng = [TiUtils floatValue:[args objectForKey:@"lng"]];
CGFloat accuracy = [TiUtils floatValue:[args objectForKey:@"accuracy"]];
CGFloat radius = [TiUtils floatValue:[args objectForKey:@"radius"]];
NSString *identifier = [TiUtils stringValue:[args objectForKey:@"identifier"]];
CLLocationManager *lm = [self locationManager];
if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) {
CLLocationCoordinate2D location;
location.latitude=lat;
location.longitude=lng;
CLLocationDistance regionRadius = radius;
CLRegion *grRegion = [[CLRegion alloc] initCircularRegionWithCenter:location radius:regionRadius identifier:identifier];
if ([lm respondsToSelector:(@selector(startMonitoringForRegion:desiredAccuracy:))]) {
[lm startMonitoringForRegion:grRegion desiredAccuracy:accuracy];
}
}
}
-(void)stopMonitoring:(id)args
{
ENSURE_UI_THREAD(stopMonitoring,args);
ENSURE_SINGLE_ARG(args,NSDictionary);
CGFloat lat = [TiUtils floatValue:[args objectForKey:@"lat"]];
CGFloat lng = [TiUtils floatValue:[args objectForKey:@"lng"]];
CGFloat radius = [TiUtils floatValue:[args objectForKey:@"radius"]];
NSString *identifier = [TiUtils stringValue:[args objectForKey:@"identifier"]];
CLLocationManager *lm = [self locationManager];
if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) {
CLLocationCoordinate2D location;
location.latitude=lat;
location.longitude=lng;
CLLocationDistance regionRadius = radius;
CLRegion *grRegion = [[CLRegion alloc] initCircularRegionWithCenter:location radius:regionRadius identifier:identifier];
if ([lm respondsToSelector:(@selector(stopMonitoringForRegion:))]) {
[lm stopMonitoringForRegion:grRegion];
}
}
}
-(id)monitoredRegions:(id)args
{
CLLocationManager *lm = [self locationManager];
NSArray *allRegions = [[lm monitoredRegions] allObjects];
NSMutableArray *regionDictionaryArray = [[NSMutableArray alloc] init];
for (id region in allRegions) {
NSDictionary *jsonRegion = [NSDictionary dictionaryWithObjectsAndKeys:NUMDOUBLE(((CLRegion *)region).center.latitude),@"lat",NUMDOUBLE(((CLRegion *)region).center.longitude),@"lng",NUMDOUBLE(((CLRegion *)region).radius),@"radius",((CLRegion *)region).identifier,@"identifier",nil];
[regionDictionaryArray addObject:jsonRegion];
}
return regionDictionaryArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment