forked from martinstolz/DMLocationManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMLocationManager.h
More file actions
executable file
·250 lines (215 loc) · 7.89 KB
/
DMLocationManager.h
File metadata and controls
executable file
·250 lines (215 loc) · 7.89 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// Copyright devmob (Martin Stolz) | devmob.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
/**
* The DMLocationManager is a convinience wrapper for the CLLocationManager.
* It accepts multiple delegate instances at one time.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* [locationManager addDelegate:self];
* [locationManager removeDelegate:self];
*
* To avoid bad accuracy of location the property 'queryingInterval' defines how long to search for a location with the desired accuracy.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
* locationManager.queryingInterval = 10.0;
*
* To avoid getting cached location coordinates deactivate it by setting NO to 'useCache' property.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* locationManager.useCache = NO;
*
* The 'cacheAge' defines how old a location is allowed to be.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* locationManager.cacheAge = 10.0;
*
* If nessecary the property 'updateLocationOnApplicationDidBecomeActive' can be used to allow the update of the location on application (re-)start.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* locationManager.updateLocationOnApplicationDidBecomeActive = YES;
*
* To keep the location permanent up to date the property 'loop' defines whether to repeadeatly determine new location coordinates.
* The property 'loopTimeInterval' defines how long to sleep between determining the location successfully and beginning the next determination.
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
* locationManager.loop = YES;
* locationManager.loopTimeInterval = 10.0;
*
*
*
* How it works:
*
* Use location manager singleton instance:
*
* DMLocationManager* locationManager = [DMLocationManager sharedLocationManager];
*
* Listen for changes by setting delegate instance:
*
* [locationManager addDelegate:self];
*
* Make listening instance optionally conform to DMLocationManagerDelegate:
*
* - (void)locationManager:(DMLocationManager*)manager didChangeLocationServiceEnabledState:(BOOL)isLocationServiceEnabled {
* }
*
* - (void)locationManagerWillUpdateLocation:(DMLocationManager*)manager {
* }
*
* - (void)locationManagerDidStopUpdateLocation:(DMLocationManager*)manager {
* }
*
* - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
* }
*
* Start determine location:
*
* [locationManager startUpdatingLocation];
*
* Stop determine location on leaving a view e.g.:
*
* [locationManager stopUpdatingLocation];
*
* Stop listening for location changes:
*
* [locationManager removeDelegate:self];
*
* Activate logging by setting the log level define e.g.:
*
* #define DM_LOCATION_MANAGER_LOG_LEVEL DM_LOCATION_MANAGER_LOG_LEVEL_INFO
*/
@protocol DMLocationManagerDelegate;
#pragma mark -
#pragma mark DMLocationManager
#define DM_LOCATION_MANAGER_LOG_LEVEL_NONE 0
#define DM_LOCATION_MANAGER_LOG_LEVEL_INFO 1
#define DM_LOCATION_MANAGER_LOG_LEVEL_WARNING 2
#define DM_LOCATION_MANAGER_LOG_LEVEL_ERROR 3
#define DM_LOCATION_MANAGER_LOG_LEVEL_DEBUG 4
#define DM_LOCATION_MANAGER_LOG_LEVEL DM_LOCATION_MANAGER_LOG_LEVEL_INFO
@interface DMLocationManager : NSObject <CLLocationManagerDelegate>
{
@private
NSMutableArray* _delegates; // Delegate instances which must be served
CLLocationManager* _locationManager; // Shared location manager instance
CLLocation* _location;
BOOL _useCache;
NSTimeInterval _cacheAge;
BOOL _isLocationServiceEnabled;
BOOL _updateLocationOnApplicationDidBecomeActive;
BOOL _loop;
NSTimeInterval _loopTimeInterval;
NSTimer* _loopTimer; // Restart searching of new locations after one was found
NSTimeInterval _queryingInterval;
NSTimer* _queryingTimer; // On timeout the updating of location will be stopped
}
/**
* Last determined location by location manager. If nil the location was or could not be updated.
*/
@property (nonatomic, retain, readonly) CLLocation* location;
/**
* The accuracy which should be aimed. If the accuracy is the desired one, the updating process will be stopped before the query time is reached.
* Default is -1.
*/
@property (nonatomic, assign) CLLocationAccuracy desiredAccuracy;
/**
* Determines how long the location manager must search for new locations with desired accuracy.
* Default is 10 seconds.
*/
@property (nonatomic, assign) NSTimeInterval queryingInterval;
/**
* Returns whether the location manager currently searches for new locations.
*/
@property (nonatomic, assign, readonly) BOOL isQuerying;
/**
* If YES the last cached location of location manager will be used, which could be some days ago.
* Default is NO.
*/
@property (nonatomic, assign) BOOL useCache;
/**
* The threshold until the location manager uses the cached location of core location manager.
* Default is 10 seconds.
*/
@property (nonatomic, assign) NSTimeInterval cacheAge;
/**
* Returns whether the location service of the device is activated. If the application is not allowed this value will be updated by the first search of new location.
*/
@property (nonatomic, assign, readonly) BOOL isLocationServiceEnabled;
/**
* If YES the delegates will be informed of location service state changes and a new location will be searched on (re-) activation of the app.
* Default is NO.
*/
@property (nonatomic, assign) BOOL updateLocationOnApplicationDidBecomeActive;
/**
* Repeat searching for new locations after a location was determined.
* Default is NO.
*/
@property (nonatomic, assign) BOOL loop;
/**
* After which amount of time after finding a location the search should be restarted
* Default is 10 seconds.
*/
@property (nonatomic, assign) NSTimeInterval loopTimeInterval;
/**
* Returns the shared instance.
*/
+ (DMLocationManager*) sharedLocationManager;
/**
* Add a delegate of kind DMLocationManagerDelegate which must be served.
*
* @see DMLocationManagerDelegate
*/
- (void)addDelegate:(id<DMLocationManagerDelegate>) delegate;
/**
* Remove a delegate of kind DMLocationManagerDelegate which must not be served.
*
* @see DMLocationManagerDelegate
*/
- (void)removeDelegate:(id<DMLocationManagerDelegate>) delegate;
/**
* Start updating the location
*/
- (void)startUpdatingLocation;
/**
* Stop updating the location
*/
- (void)stopUpdatingLocation;
@end
#pragma mark -
#pragma mark DMLocationManagerDelegate
@protocol DMLocationManagerDelegate <CLLocationManagerDelegate>
@optional
/**
* Informs about changes of location service state.
*
* @see DMLocationManager
*/
- (void)locationManager:(DMLocationManager*)manager didChangeLocationServiceEnabledState:(BOOL)isLocationServiceEnabled;
/**
* Informs when new locations will be started to search for.
*
* @see DMLocationManager
*/
- (void)locationManagerWillUpdateLocation:(DMLocationManager*)manager;
/**
* Informs about stop searching new locations.
*
* @see DMLocationManager
*/
- (void)locationManagerDidStopUpdateLocation:(DMLocationManager*)manager;
@end