NativeLocation.mm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // NativeLocation.m
  3. // Immersal SDK
  4. //
  5. // Created by Mikko on 29/05/2020.
  6. //
  7. //
  8. #import "NativeLocation.h"
  9. double latitude;
  10. double longitude;
  11. double altitude;
  12. double haccuracy;
  13. double vaccuracy;
  14. @implementation NativeLocation
  15. CLLocationManager *locationManager;
  16. static bool isEnabled = NO;
  17. - (NativeLocation *)init
  18. {
  19. locationManager = [[CLLocationManager alloc] init];
  20. locationManager.delegate = self;
  21. locationManager.distanceFilter = kCLDistanceFilterNone;
  22. locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  23. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
  24. [locationManager requestWhenInUseAuthorization];
  25. return self;
  26. }
  27. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
  28. {
  29. /* switch (status) {
  30. case kCLAuthorizationStatusAuthorizedWhenInUse:
  31. case kCLAuthorizationStatusAuthorizedAlways:
  32. isEnabled = YES; break;
  33. default:
  34. isEnabled = NO; break;
  35. }*/
  36. }
  37. - (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error;
  38. {
  39. isEnabled = NO;
  40. }
  41. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
  42. {
  43. CLLocation *location = [locations lastObject];
  44. latitude = location.coordinate.latitude;
  45. longitude = location.coordinate.longitude;
  46. altitude = location.altitude;
  47. haccuracy = location.horizontalAccuracy;
  48. vaccuracy = location.verticalAccuracy;
  49. isEnabled = YES;
  50. //NSLog(@"lat: %f long: %f alt: %f", latitude, longitude, altitude);
  51. }
  52. - (void)start
  53. {
  54. if (locationManager != NULL) {
  55. [locationManager startUpdatingLocation];
  56. }
  57. }
  58. - (void)stop
  59. {
  60. if (locationManager != NULL) {
  61. [locationManager stopUpdatingLocation];
  62. }
  63. isEnabled = NO;
  64. }
  65. @end
  66. static NativeLocation* locationDelegate = NULL;
  67. extern "C"
  68. {
  69. void startLocation()
  70. {
  71. if (locationDelegate == NULL) {
  72. locationDelegate = [[NativeLocation alloc] init];
  73. }
  74. [locationDelegate start];
  75. }
  76. void stopLocation()
  77. {
  78. if (locationDelegate != NULL) {
  79. [locationDelegate stop];
  80. }
  81. }
  82. double getLatitude()
  83. {
  84. return latitude;
  85. }
  86. double getLongitude()
  87. {
  88. return longitude;
  89. }
  90. double getAltitude()
  91. {
  92. return altitude;
  93. }
  94. double getHorizontalAccuracy()
  95. {
  96. return haccuracy;
  97. }
  98. double getVerticalAccuracy()
  99. {
  100. return vaccuracy;
  101. }
  102. bool locationServicesEnabled()
  103. {
  104. return isEnabled;
  105. }
  106. }