-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUULocationManager.m
215 lines (181 loc) · 6.33 KB
/
UULocationManager.m
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// UULocationManager.m
// Useful Utilities - CLLocationManager wrapper
//
// License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
#import "UULocationManager.h"
#if __has_feature(objc_arc)
#define UU_RELEASE(x) (void)(0)
#define UU_RETAIN(x) x
#define UU_AUTORELEASE(x) x
#define UU_BLOCK_RELEASE(x) (void)(0)
#define UU_BLOCK_COPY(x) [x copy]
#else
#define UU_RELEASE(x) [x release]
#define UU_RETAIN(x) [x retain]
#define UU_AUTORELEASE(x) [(x) autorelease]
#define UU_BLOCK_RELEASE(x) Block_release(x)
#define UU_BLOCK_COPY(x) Block_copy(x)
#endif
NSString * const UULocationChangedNotification = @"UULocationChangedNotification";
NSString * const UULocationNameChangedNotification = @"UULocationNameChangedNotification";
NSString * const UULocationAuthChangedNotification = @"UULocationAuthChangedNotification";
NSString * const UULocationErrorNotification = @"UULocationErrorNotification";
@interface UULocationManager()
@property (nonatomic, retain) CLLocationManager* clLocationManager;
@property (nonatomic, retain) CLLocation* clLocation;
@property (nonatomic, retain) NSString* locationName;
@property (nonatomic, retain) NSString* cityName;
@property (nonatomic, retain) NSString* stateName;
@property (nonatomic, retain) NSTimer* notificationTimer;
@end
static UULocationManager* theLocationManager = nil;
@implementation UULocationManager
+ (UULocationManager*) sharedInstance
{
if (theLocationManager == nil)
{
theLocationManager = [[UULocationManager alloc] init];
}
return theLocationManager;
}
+ (void) startTracking
{
//Just call the accessor...
[UULocationManager sharedInstance];
}
- (CLLocation*) currentLocation
{
return self.clLocation;
}
- (NSString*) currentLocationName
{
return self.locationName;
}
- (NSString*) currentCityName
{
return self.cityName;
}
- (NSString*) currentStateName
{
return self.stateName;
}
- (id) init
{
self = [super init];
if (self)
{
self.distanceThreshold = 10.0f; // 10 meters
self.timeThreshold = 1800; // 30 minutes
self.monitorLocationName = NO;
self.delayLocationUpdates = NO;
self.locationUpdateDelay = 1.0f; // 1 second
if ([CLLocationManager locationServicesEnabled])
{
// Initialize the locationManager
CLLocationManager* locManager = UU_AUTORELEASE([[CLLocationManager alloc] init]);
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
locManager.distanceFilter = self.distanceThreshold;
self.clLocationManager = locManager;
//Go!!!
[self.clLocationManager startUpdatingLocation];
}
}
return self;
}
- (void) startTracking
{
[self.clLocationManager startUpdatingLocation];
}
- (void) stopTracking
{
self.clLocation = nil;
[self.clLocationManager stopUpdatingLocation];
}
- (void) startTrackingSignificantLocationChanges
{
[self.clLocationManager startMonitoringSignificantLocationChanges];
}
- (void) stopTrackingSignificantLocationChanges
{
[self.clLocationManager stopMonitoringSignificantLocationChanges];
}
- (void) setDistanceThreshold:(CLLocationDistance)distanceThreshold
{
_distanceThreshold = distanceThreshold;
self.clLocationManager.distanceFilter = distanceThreshold;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
{
if (!self.clLocation ||
!CLLocationCoordinate2DIsValid(self.clLocation.coordinate) ||
[self.clLocation distanceFromLocation:newLocation] > self.distanceThreshold ||
[self.clLocation.timestamp timeIntervalSinceDate:newLocation.timestamp] > self.timeThreshold ||
newLocation.horizontalAccuracy < self.clLocation.horizontalAccuracy)
{
self.clLocation = newLocation;
if (self.delayLocationUpdates)
{
[self.notificationTimer invalidate];
self.notificationTimer = [NSTimer scheduledTimerWithTimeInterval:self.locationUpdateDelay target:self selector:@selector(postLocationChangedTimer:) userInfo:self.clLocation repeats:NO];
}
else
{
[[NSNotificationCenter defaultCenter] postNotificationName:UULocationChangedNotification object:self.clLocation];
}
if (self.monitorLocationName)
{
[self queryLocationName:self.clLocation];
}
}
}
- (void) postLocationChangedTimer:(NSTimer*)timer
{
CLLocation* location = timer.userInfo;
[[NSNotificationCenter defaultCenter] postNotificationName:UULocationChangedNotification object:location];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[[NSNotificationCenter defaultCenter] postNotificationName:UULocationErrorNotification object:error];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
[[NSNotificationCenter defaultCenter] postNotificationName:UULocationAuthChangedNotification object:@(status)];
}
- (void) queryLocationName:(CLLocation*)location
{
CLGeocoder* geoCoder = UU_AUTORELEASE([[CLGeocoder alloc] init]);
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError* error)
{
if (error == nil && placemarks != nil && placemarks.count > 0)
{
CLPlacemark* info = [placemarks objectAtIndex:0];
NSMutableString* sb = [NSMutableString string];
if (info.locality != nil)
{
[sb appendString:info.locality];
self.cityName = info.locality;
}
if (info.administrativeArea != nil)
{
if (sb.length > 0)
{
[sb appendString:@", "];
}
[sb appendString:info.administrativeArea];
self.stateName = info.administrativeArea;
}
self.locationName = sb;
[[NSNotificationCenter defaultCenter] postNotificationName:UULocationNameChangedNotification object:sb];
}
}];
}
- (bool) hasValidLocation
{
return ((self.clLocation != nil) && CLLocationCoordinate2DIsValid(self.clLocation.coordinate));
}
@end