summaryrefslogtreecommitdiff
path: root/platform/ios/MGLMapboxEvents.m
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-05-09 00:19:20 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-05-12 01:36:56 -0700
commit2f02a6538e8b13b77072045c9872b0169e10afcc (patch)
treef6795a4a88b5d7bb0180cd51a151e3511f8812bb /platform/ios/MGLMapboxEvents.m
parentc1fe6e18907b4edcb27fd7378091123e90d4e68f (diff)
downloadqtlocation-mapboxgl-2f02a6538e8b13b77072045c9872b0169e10afcc.tar.gz
Folded MGLMetricsLocationManager into MGLMapboxEvents
`MGLMetricsLocationManager` was nothing but overhead for `CLLocationManager`. Also removed an unused method.
Diffstat (limited to 'platform/ios/MGLMapboxEvents.m')
-rw-r--r--platform/ios/MGLMapboxEvents.m83
1 files changed, 73 insertions, 10 deletions
diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m
index 8eefd7c058..74693f5c04 100644
--- a/platform/ios/MGLMapboxEvents.m
+++ b/platform/ios/MGLMapboxEvents.m
@@ -4,9 +4,9 @@
#import <SystemConfiguration/CaptiveNetwork.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
+#import <CoreLocation/CoreLocation.h>
#import "MGLAccountManager.h"
-#import "MGLMetricsLocationManager.h"
#import "NSProcessInfo+MGLAdditions.h"
#import "NSBundle+MGLAdditions.h"
#import "NSException+MGLAdditions.h"
@@ -130,7 +130,7 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
// strong references.
//
-@interface MGLMapboxEvents()
+@interface MGLMapboxEvents () <CLLocationManagerDelegate>
// All of the following properties are written to only from
// the main thread, but can be read on any thread.
@@ -140,13 +140,15 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
@property (atomic) NSString *appName;
@property (atomic) NSString *appVersion;
@property (atomic) NSString *appBuildNumber;
-@property (atomic) MGLMetricsLocationManager *locationManager;
@property (atomic) NSUInteger flushAt;
@property (atomic) NSDateFormatter *rfc3339DateFormatter;
@property (atomic) NSURLSession *session;
@property (atomic) NSData *digicertCert;
@property (atomic) NSData *geoTrustCert;
+// Main thread only
+@property (nonatomic) CLLocationManager *locationManager;
+
// The paused state tracker is only ever accessed from the main thread.
//
@property (nonatomic, getter=isPaused) BOOL paused;
@@ -299,6 +301,18 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
_data = nil;
[_session invalidateAndCancel];
_session = nil;
+
+ [self stopUpdatingLocation];
+}
+
+- (void)stopUpdatingLocation {
+ [_locationManager stopUpdatingLocation];
+
+ // -[CLLocationManager stopMonitoringVisits] is only available in iOS 8+.
+ if ([_locationManager respondsToSelector:@selector(stopMonitoringVisits)]) {
+ [_locationManager stopMonitoringVisits];
+ }
+
_locationManager = nil;
}
@@ -316,7 +330,22 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
self.paused = NO;
_data = [[MGLMapboxEventsData alloc] init];
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
- _locationManager = [[MGLMetricsLocationManager alloc] init];
+
+ [self startUpdatingLocation];
+}
+
+- (void)startUpdatingLocation {
+ _locationManager = [[CLLocationManager alloc] init];
+ _locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
+ _locationManager.distanceFilter = 10;
+ _locationManager.delegate = self;
+
+ [_locationManager startUpdatingLocation];
+
+ // -[CLLocationManager startMonitoringVisits] is only available in iOS 8+.
+ if ([_locationManager respondsToSelector:@selector(startMonitoringVisits)]) {
+ [_locationManager startMonitoringVisits];
+ }
}
// Can be called from any thread. Can be called rapidly from
@@ -495,12 +524,6 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
// Can be called from any thread.
//
-- (NSString *) formatDate:(NSDate *)date {
- return [self.rfc3339DateFormatter stringFromDate:date];
-}
-
-// Can be called from any thread.
-//
- (NSString *) deviceOrientation {
__block NSString *result;
@@ -678,6 +701,46 @@ NSString *const MGLEventGestureRotateStart = @"Rotation";
return result;
}
+#pragma mark CLLocationManagerDelegate
+- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
+ // Iterate through locations to pass all data
+ for (CLLocation *loc in locations) {
+ [MGLMapboxEvents pushEvent:MGLEventTypeLocation withAttributes:@{
+ MGLEventKeyLatitude: @(loc.coordinate.latitude),
+ MGLEventKeyLongitude: @(loc.coordinate.longitude),
+ MGLEventKeySpeed: @(loc.speed),
+ MGLEventKeyCourse: @(loc.course),
+ MGLEventKeyAltitude: @(loc.altitude),
+ MGLEventKeyHorizontalAccuracy: @(loc.horizontalAccuracy),
+ MGLEventKeyVerticalAccuracy: @(loc.verticalAccuracy)
+ }];
+ }
+}
+
+- (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 {
+ switch (status) {
+ case kCLAuthorizationStatusDenied:
+ [self stopUpdatingLocation];
+ break;
+ case kCLAuthorizationStatusAuthorized:
+ case kCLAuthorizationStatusAuthorizedWhenInUse:
+ [self startUpdatingLocation];
+ break;
+ default:
+ break;
+ }
+}
+
#pragma mark NSURLSessionDelegate
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^) (NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {