LocationPlugin.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // LocationPlugin.mm
  2. #import <Foundation/Foundation.h>
  3. #import <CoreLocation/CoreLocation.h>
  4. typedef void (*ResultHandler) (const char *object);
  5. static ResultHandler gHandler;
  6. @interface LocationPlugin : NSObject<CLLocationManagerDelegate>
  7. // 采用CLLocationManagerDelegate协议
  8. {
  9. CLLocationManager *locationManager;
  10. bool isReverseGeocoding; // 是否处于反地理编码处理
  11. }
  12. + (LocationPlugin *)sharedInstance;
  13. @end
  14. // LocationPlugin类定义
  15. @implementation LocationPlugin
  16. // 单例
  17. static LocationPlugin *sharedInstance = nil;
  18. + (id)sharedInstance
  19. {
  20. @synchronized(self)
  21. {
  22. if(sharedInstance == nil)
  23. {
  24. sharedInstance = [[self alloc] init];
  25. }
  26. }
  27. return sharedInstance;
  28. }
  29. // 开始获取位置信息
  30. - (BOOL)startUpdatingLocation
  31. {
  32. if(locationManager == nil)
  33. {
  34. locationManager = [[CLLocationManager alloc] init];
  35. }
  36. // 确认位置信息服务是否有效且被许可
  37. BOOL isEnabledAndAuthorized = NO;
  38. if([CLLocationManager locationServicesEnabled])
  39. {
  40. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  41. if(status == kCLAuthorizationStatusAuthorizedAlways ||
  42. status == kCLAuthorizationStatusAuthorizedWhenInUse)
  43. {
  44. isEnabledAndAuthorized = YES;
  45. }
  46. }
  47. if(!isEnabledAndAuthorized)
  48. {
  49. // 位置信息服务无效且未被许可时,请求用户授权
  50. [locationManager requestWhenInUseAuthorization];
  51. return NO;
  52. }
  53. // 开始获取位置信息
  54. locationManager.delegate = self;
  55. locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  56. [locationManager startUpdatingLocation];
  57. return YES;
  58. }
  59. // 停止获取位置信息
  60. - (void)stopUpdatingLocation
  61. {
  62. if(locationManager != nil)
  63. {
  64. [locationManager stopUpdatingLocation];
  65. }
  66. }
  67. #pragma mark - CLLocationManagerDelegate协议的方法实现
  68. // 更新位置信息时调用
  69. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  70. {
  71. if(isReverseGeocoding)
  72. {
  73. return;
  74. }
  75. // 基于获取的位置信息进行反地理编码以获取地址
  76. isReverseGeocoding = YES;
  77. CLLocation *location = [locations lastObject];
  78. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  79. [geocoder reverseGeocodeLocation:location
  80. completionHandler:^(NSArray *placemarks, NSError *error) {
  81. isReverseGeocoding = NO;
  82. // 从CLPlacemark对象的FormattedAddressLines中获取地址的字符串
  83. NSString *addressString = @"";
  84. NSString *addressOut = @"";
  85. if(placemarks.count >= 1)
  86. {
  87. CLPlacemark *placemark = [placemarks firstObject];
  88. NSArray *addressLines =
  89. [placemark addressDictionary][@"FormattedAddressLines"];
  90. addressString = [addressLines componentsJoinedByString:@" "];
  91. addressOut = [NSString stringWithFormat:@"%@\t%@\t%@",placemark.country,placemark.administrativeArea,placemark.locality];
  92. }
  93. // 创建作为参数传递的字符串
  94. NSString *parameter = [NSString stringWithFormat:@"%f\t%f\t%f\t%f\t%@\t%@",
  95. location.coordinate.latitude,
  96. location.coordinate.longitude,
  97. location.speed, location.horizontalAccuracy,
  98. addressString,addressOut];
  99. gHandler (parameter.UTF8String);
  100. }];
  101. }
  102. //监听权限变化
  103. - (void) locationManagerDidChangeAuthorization:(CLLocationManager *)manager{
  104. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  105. NSLog(@"locationManagerDidChangeAuthorizatio1:%d",status);
  106. if (status == kCLAuthorizationStatusAuthorizedAlways
  107. || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
  108. NSLog(@"locationManagerDidChangeAuthorizatio2:%@",@"11111");
  109. }
  110. }
  111. // 其他必须的方法
  112. - (void)locationManager:(CLLocationManager *)manager
  113. didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {}
  114. - (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager {}
  115. - (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {}
  116. @end
  117. #pragma mark - 接口
  118. // 为回避C++命名粉碎,以C linkage进行声明
  119. extern "C" {
  120. // 用于调用开始获取位置信息方法的接口
  121. BOOL _startUpdatingLocation(ResultHandler resultHandler)
  122. {
  123. gHandler = resultHandler;
  124. LocationPlugin *instance = [LocationPlugin sharedInstance];
  125. @synchronized(instance)
  126. {
  127. return [instance startUpdatingLocation];
  128. }
  129. }
  130. // 用于调用停止获取位置信息方法的接口
  131. void _stopUpdatingLocation()
  132. {
  133. LocationPlugin *instance = [LocationPlugin sharedInstance];
  134. @synchronized(instance)
  135. {
  136. [instance stopUpdatingLocation];
  137. }
  138. }
  139. }