SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSiOSGpsDelegate.mm
Go to the documentation of this file.
1 #import "SENSiOSGpsDelegate.h"
2 
3 @interface SENSiOSGpsDelegate () {
4 
5 @private
6  CLLocationManager* _locationManager;
7  BOOL _running;
8 }
9 
10 @end
11 
12 @implementation SENSiOSGpsDelegate
13 
14 - (id)init
15 {
16  //Initialize the parent class(es) up the hierarchy and create self:
17  self = [super init];
18 
19  //Initialize members (not necessary with ARC)
20  _locationManager = nil;
21  _running = NO;
22  if (self)
23  {
24  [self setupLocationManager];
25  }
26 
27  return self;
28 }
29 
30 //! Starts the location data update if the interval time > 0 else it stops
32 {
33  // Init location manager
34  _locationManager = [[CLLocationManager alloc] init];
35  _locationManager.delegate = self;
36  _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
37 
38  [self askPermission];
39 }
40 
42 {
43  // for iOS 8, specific user level permission is required,
44  // "when-in-use" authorization grants access to the user's location.
45  // important: be sure to include NSLocationWhenInUseUsageDescription along with its
46  // explanation string in your Info.plist or startUpdatingLocation will not work
47  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
48  {
49  [_locationManager requestWhenInUseAuthorization];
50  }
51 }
52 
53 //! Starts the location data update
54 - (BOOL)start
55 {
56  if (_locationManager == nil)
57  return NO;
58 
59  if (!_running)
60  {
61  [_locationManager startUpdatingLocation];
62  _running = true;
63  printf("Starting Location Manager\n");
64  }
65 
66  return YES;
67 }
68 //-----------------------------------------------------------------------------
69 //! Stops the location data update
70 - (void)stop
71 {
72  if (_running)
73  {
74  [_locationManager stopUpdatingLocation];
75  _running = false;
76  printf("Stopping Location Manager\n");
77  }
78 }
79 //-----------------------------------------------------------------------------
80 - (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
81 {
82  //printf("horizontalAccuracy: %f\n", newLocation.horizontalAccuracy);
83 
84  // negative horizontal accuracy means no location fix
85  if (newLocation.horizontalAccuracy > 0.0)
86  {
87  //callback here
88  if (_updateCB)
89  {
90  _updateCB(newLocation.coordinate.latitude,
91  newLocation.coordinate.longitude,
92  newLocation.altitude,
93  newLocation.horizontalAccuracy);
94  }
95  }
96 }
97 //-----------------------------------------------------------------------------
98 - (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error
99 {
100  // The location "unknown" error simply means the manager is currently unable to get the location.
101  // We can ignore this error for the scenario of getting a single location fix, because we already have a
102  // timeout that will stop the location manager to save power.
103  //
104  if ([error code] != kCLErrorLocationUnknown)
105  {
106  printf("**** locationManager didFailWithError ****\n");
107  [self stop];
108  }
109 }
110 
111 - (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
112 {
113  //callback permission status
114  if (status == kCLAuthorizationStatusAuthorizedAlways ||
115  status == kCLAuthorizationStatusAuthorizedWhenInUse)
116  {
117  if (_permissionCB)
118  _permissionCB(true);
119  }
120  else
121  {
122  if (_permissionCB)
123  _permissionCB(false);
124  }
125 }
126 
127 @end
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
CLLocationManager * _locationManager
void stop()
Stops the location data update.
BOOL start()
Starts the location data update.
void setupLocationManager()
Starts the location data update if the interval time > 0 else it stops.