summaryrefslogtreecommitdiff
path: root/platform/ios/MGLMetricsLocationManager.m
blob: 19210fddcac97604dda5ac525b4cb53409737ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
//  MBLocationManager.m
//  Hermes
//
//  Dynamic Settings.bundle loading based on:
//  http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the
//
//  Created by Brad Leege on 3/8/15.
//  Copyright (c) 2015 Mapbox. All rights reserved.
//

#import "MGLMetricsLocationManager.h"
#import "CoreLocation/CoreLocation.h"
#import "MGLMapboxEvents.h"

@interface MGLMetricsLocationManager()

@property (atomic) CLLocationManager *locationManager;

@end

@implementation MGLMetricsLocationManager

static MGLMetricsLocationManager *sharedManager = nil;

- (id) init {
    if (self = [super init]) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.distanceFilter = 2;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.pausesLocationUpdatesAutomatically = YES;
        [_locationManager setDelegate:self];
    }
    return self;
}

+ (id)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (BOOL) isAuthorizedStatusDetermined {
    return ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined);
}

- (void) requestAlwaysAuthorization {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    } else {
        // This is iOS 7 or below so Starting Location Updates will trigger authorization request
        [self startUpdatingLocation];
    }
}

- (void) startUpdatingLocation {
    [self.locationManager startUpdatingLocation];
}

- (void) stopUpdatingLocation {
    [self.locationManager stopUpdatingLocation];
}

#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    //  Iterate through locations to pass all data
    for (CLLocation *loc in locations) {
        NSMutableDictionary *evt = [[NSMutableDictionary alloc] init];
        [evt setValue:[[NSNumber alloc] initWithDouble:loc.coordinate.latitude] forKey:@"lat"];
        [evt setValue:[[NSNumber alloc] initWithDouble:loc.coordinate.longitude] forKey:@"lng"];
        [[MGLMapboxEvents sharedManager] pushEvent:@"location" withAttributes:evt];
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSString *newStatus = nil;
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            newStatus = @"User Hasn't Determined Yet";
            break;
        case kCLAuthorizationStatusRestricted:
            newStatus = @"Restricted and Can't Be Changed By User";
            break;
        case kCLAuthorizationStatusDenied:
            newStatus = @"User Explcitly Denied";
            [[MGLMetricsLocationManager sharedManager] stopUpdatingLocation];
            break;
        case kCLAuthorizationStatusAuthorized:
            newStatus = @"User Has Authorized / Authorized Always";
            [[MGLMetricsLocationManager sharedManager] startUpdatingLocation];
            break;
            //        case kCLAuthorizationStatusAuthorizedAlways:
            //            newStatus = @"Not Determined";
            //            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            newStatus = @"User Has Authorized When In Use Only";
            [[MGLMetricsLocationManager sharedManager] startUpdatingLocation];
            break;
        default:
            newStatus = @"Unknown";
            break;
    }
}

@end