Merge pull request #1356 from mapbox/1352-visits

Visit API
This commit is contained in:
Brad Leege
2015-04-27 21:32:15 -04:00
4 changed files with 45 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ extern NSString *const MGLEventTypeMapLoad;
extern NSString *const MGLEventTypeMapTap;
extern NSString *const MGLEventTypeMapDragEnd;
extern NSString *const MGLEventTypeLocation;
extern NSString *const MGLEventTypeVisit;
extern NSString *const MGLEventKeyLatitude;
extern NSString *const MGLEventKeyLongitude;
@@ -16,6 +17,8 @@ extern NSString *const MGLEventKeyVerticalAccuracy;
extern NSString *const MGLEventKeyPushEnabled;
extern NSString *const MGLEventKeyEmailEnabled;
extern NSString *const MGLEventKeyGestureID;
extern NSString *const MGLEventKeyArrivalDate;
extern NSString *const MGLEventKeyDepartureDate;
extern NSString *const MGLEventGestureSingleTap;
extern NSString *const MGLEventGestureDoubleTap;

View File

@@ -7,5 +7,7 @@
+ (instancetype)sharedManager;
+ (void) startUpdatingLocation;
+ (void) stopUpdatingLocation;
+ (void) startMonitoringVisits;
+ (void) stopMonitoringVisits;
@end

View File

@@ -17,6 +17,7 @@ NSString *const MGLEventTypeMapLoad = @"map.load";
NSString *const MGLEventTypeMapTap = @"map.click";
NSString *const MGLEventTypeMapDragEnd = @"map.dragend";
NSString *const MGLEventTypeLocation = @"location";
NSString *const MGLEventTypeVisit = @"visit";
NSString *const MGLEventKeyLatitude = @"lat";
NSString *const MGLEventKeyLongitude = @"lng";
@@ -29,6 +30,8 @@ NSString *const MGLEventKeyVerticalAccuracy = @"verticalAccuracy";
NSString *const MGLEventKeyPushEnabled = @"enabled.push";
NSString *const MGLEventKeyEmailEnabled = @"enabled.email";
NSString *const MGLEventKeyGestureID = @"gesture";
NSString *const MGLEventKeyArrivalDate = @"arrivalDate";
NSString *const MGLEventKeyDepartureDate = @"departureDate";
NSString *const MGLEventGestureSingleTap = @"SingleTap";
NSString *const MGLEventGestureDoubleTap = @"DoubleTap";
@@ -271,6 +274,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
}
[MGLMapboxEvents sharedManager].isPaused = YES;
[MGLMetricsLocationManager stopUpdatingLocation];
[MGLMetricsLocationManager stopMonitoringVisits];
}
// Must be called from the main thread.
@@ -282,6 +286,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
}
[MGLMapboxEvents sharedManager].isPaused = NO;
[MGLMetricsLocationManager startUpdatingLocation];
[MGLMetricsLocationManager startMonitoringVisits];
}
// Can be called from any thread. Can be called rapidly from

View File

@@ -6,6 +6,7 @@
@interface MGLMetricsLocationManager() <CLLocationManagerDelegate>
@property (nonatomic) CLLocationManager *locationManager;
@property (atomic) NSDateFormatter *rfc3339DateFormatter;
@end
@@ -16,6 +17,15 @@
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = 10;
[_locationManager setDelegate:self];
_rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[_rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[_rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
// Clear Any System TimeZone Cache
[NSTimeZone resetSystemTimeZone];
[_rfc3339DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
}
return self;
}
@@ -37,6 +47,18 @@
[[MGLMetricsLocationManager sharedManager].locationManager stopUpdatingLocation];
}
+ (void) startMonitoringVisits {
if ([[MGLMetricsLocationManager sharedManager].locationManager respondsToSelector:@selector(startMonitoringVisits)]) {
[[MGLMetricsLocationManager sharedManager].locationManager startMonitoringVisits];
}
}
+ (void) stopMonitoringVisits {
if ([[MGLMetricsLocationManager sharedManager].locationManager respondsToSelector:@selector(stopMonitoringVisits)]) {
[[MGLMetricsLocationManager sharedManager].locationManager stopMonitoringVisits];
}
}
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// Iterate through locations to pass all data
@@ -53,6 +75,16 @@
}
}
- (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit {
[MGLMapboxEvents pushEvent:MGLEventTypeVisit withAttributes:@{
MGLEventKeyLatitude: @(visit.coordinate.latitude),
MGLEventKeyLongitude: @(visit.coordinate.longitude),
MGLEventKeyHorizontalAccuracy: @(visit.horizontalAccuracy),
MGLEventKeyArrivalDate: [[NSDate distantPast] isEqualToDate:visit.arrivalDate] ? [NSNull null] : [_rfc3339DateFormatter stringFromDate:visit.arrivalDate],
MGLEventKeyDepartureDate: [[NSDate distantFuture] isEqualToDate:visit.departureDate] ? [NSNull null] : [_rfc3339DateFormatter stringFromDate:visit.departureDate]
}];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSString *newStatus = nil;
switch (status) {
@@ -65,10 +97,12 @@
case kCLAuthorizationStatusDenied:
newStatus = @"User Explcitly Denied";
[MGLMetricsLocationManager stopUpdatingLocation];
[MGLMetricsLocationManager stopMonitoringVisits];
break;
case kCLAuthorizationStatusAuthorized:
newStatus = @"User Has Authorized / Authorized Always";
[MGLMetricsLocationManager startUpdatingLocation];
[MGLMetricsLocationManager startMonitoringVisits];
break;
// case kCLAuthorizationStatusAuthorizedAlways:
// newStatus = @"Not Determined";
@@ -76,6 +110,7 @@
case kCLAuthorizationStatusAuthorizedWhenInUse:
newStatus = @"User Has Authorized When In Use Only";
[MGLMetricsLocationManager startUpdatingLocation];
[MGLMetricsLocationManager startMonitoringVisits];
break;
default:
newStatus = @"Unknown";