RDSMapHelper.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // RDSMapHelper.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/9/11.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "RDSMapHelper.h"
  9. #import <CoreLocation/CoreLocation.h>
  10. //#import <BaiduMapAPI/BMapKit.h>// 百度地图库
  11. #import <MapKit/MapKit.h>// 苹果自动地图库
  12. @interface RDSMapHelper ()<CLLocationManagerDelegate> {
  13. // BMKLocationService *_locService;
  14. // BMKGeoCodeSearch* _geocodesearch;
  15. // BOOL bGeoCodeSearch,bConfiguration;
  16. float _latitude;
  17. float _longitude;
  18. }
  19. @property (strong,nonatomic) CLLocationManager* locationManager;
  20. @property (nonatomic, copy) void(^rdsLocationCompleted)(RDSUserLocation *userLocation); // 定位成功的回调
  21. @end
  22. @implementation RDSMapHelper
  23. + (instancetype)shareHelper {
  24. static RDSMapHelper *bmHelper;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. bmHelper = [[RDSMapHelper alloc] init];
  28. });
  29. return bmHelper;
  30. }
  31. - (void)rds_startLocationSevice:(void (^)(RDSUserLocation *))rdsLocationCompleted {
  32. _rdsLocationCompleted = rdsLocationCompleted;
  33. // RDS_WEAKSELF(weakSelf)
  34. // dispatch_async(dispatch_get_main_queue(), ^{
  35. //[weakSelf p_setupBaiduMap];
  36. [self p_setupLocationMap];
  37. // });
  38. }
  39. #pragma mark - 苹果地图
  40. - (void)p_setupLocationMap{
  41. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied ||
  42. [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
  43. BLOCK_SAFE_RUN(_rdsLocationCompleted, nil)
  44. return ;
  45. }
  46. if(!_locationManager){
  47. self.locationManager = [[CLLocationManager alloc] init];
  48. self.locationManager.delegate = self;
  49. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  50. self.locationManager.distanceFilter = 100.0f;
  51. _userLocation = [[RDSUserLocation alloc] init];
  52. }
  53. if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){
  54. [self.locationManager requestWhenInUseAuthorization];
  55. }
  56. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  57. // _locationManager.allowsBackgroundLocationUpdates =YES;
  58. }
  59. [self.locationManager startUpdatingLocation];
  60. }
  61. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  62. switch (status) {
  63. case kCLAuthorizationStatusNotDetermined:
  64. if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
  65. [self.locationManager requestWhenInUseAuthorization];
  66. }
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  73. CLLocation *newLocation = locations[0];
  74. CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
  75. NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
  76. [manager stopUpdatingLocation];
  77. RDS_WEAKSELF(weakSelf)
  78. CLGeocoder *geocoder = [[CLGeocoder alloc]init];
  79. [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
  80. for (CLPlacemark *place in placemarks) {
  81. NSLog(@"国家,%@",place.country); // 国家
  82. NSLog(@"省份,%@",place.administrativeArea); // 省份
  83. NSLog(@"城市,%@",place.locality); // 城市
  84. NSLog(@"区,%@",place.subLocality); // 区
  85. NSLog(@"位置名,%@",place.name); // 位置名
  86. NSLog(@"thoroughfare,%@",place.thoroughfare); // 街道
  87. NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道
  88. // KVC
  89. [weakSelf.userLocation setValue:place.administrativeArea forKeyPath:@"provinceString"];
  90. [weakSelf.userLocation setValue:place.locality forKeyPath:@"cityString"];
  91. [weakSelf.userLocation setValue:place.subLocality forKeyPath:@"districtString"];
  92. [weakSelf.userLocation setValue:place.name forKeyPath:@"detailString"];
  93. if (kNULLString(place.locality)) {
  94. NSLog(@"定位失败");
  95. }
  96. }
  97. weakSelf.userLocation.error = nil;
  98. [weakSelf p_runBlock];
  99. }];
  100. }
  101. #pragma mark - 百度地图
  102. //- (void)p_setupBaiduMap {
  103. //
  104. // if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied ||
  105. // [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
  106. // BLOCK_SAFE_RUN(_rdsLocationCompleted, nil)
  107. // return ;
  108. // }
  109. //
  110. // // 要使用百度地图,请先启动BaiduMapManager
  111. // if (!bConfiguration) {
  112. //
  113. // BMKMapManager *mapManager = [[BMKMapManager alloc]init];
  114. // bConfiguration = [mapManager start:@"BaiduAppKey" generalDelegate:nil];
  115. // }
  116. //
  117. // if (!bConfiguration) {
  118. // DDLog(@"manager start failed!");
  119. // }
  120. //
  121. // //设置定位精确度,默认:kCLLocationAccuracyBest
  122. // [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
  123. // //指定最小距离更新(米),默认:kCLDistanceFilterNone
  124. // [BMKLocationService setLocationDistanceFilter:100.f];
  125. //
  126. // //初始化BMKLocationService
  127. // _locService = [[BMKLocationService alloc]init];
  128. // _locService.delegate = self;
  129. // _userLocation = [[RDSUserLocation alloc] init];
  130. //
  131. // _geocodesearch = [[BMKGeoCodeSearch alloc] init];
  132. // _geocodesearch.delegate = self;
  133. //
  134. // //启动LocationService
  135. // [_locService startUserLocationService];
  136. //}
  137. //
  138. //#pragma mark - 百度地图 代理
  139. //
  140. ///**
  141. // *用户位置更新后,会调用此函数
  142. // *@param userLocation 新的用户位置
  143. // */
  144. //- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
  145. //{
  146. // DDLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,
  147. // userLocation.location.coordinate.longitude);
  148. //
  149. // _latitude = userLocation.location.coordinate.latitude;
  150. // _longitude = userLocation.location.coordinate.longitude;
  151. //
  152. // [_userLocation setValue:@(_latitude) forKeyPath:@"latitude"];
  153. // [_userLocation setValue:@(_longitude) forKeyPath:@"longitude"];
  154. //
  155. // CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
  156. // if (_latitude != 0 && _longitude != 0) {
  157. // pt = (CLLocationCoordinate2D){_latitude, _longitude};
  158. // }
  159. // BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
  160. // reverseGeocodeSearchOption.reverseGeoPoint = pt;
  161. // bGeoCodeSearch = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
  162. // if(bGeoCodeSearch)
  163. // {
  164. // DDLog(@"反geo检索发送成功");
  165. // }
  166. // else
  167. // {
  168. // DDLog(@"反geo检索发送失败");
  169. // _userLocation.error = [NSError errorWithDomain:@"反geo检索发送失败" code:-1 userInfo:nil];
  170. // [self p_runBlock];
  171. // }
  172. //
  173. // [_locService stopUserLocationService];
  174. //}
  175. //
  176. //
  177. //- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher
  178. // result:(BMKReverseGeoCodeResult *)result
  179. // errorCode:(BMKSearchErrorCode)error {
  180. //
  181. // if (error == 0) {
  182. // DDLog(@"~~~result:%@,%@,%@,%@%@",result.addressDetail.province,
  183. // result.addressDetail.city,
  184. // result.addressDetail.district,
  185. // result.addressDetail.streetName,
  186. // result.addressDetail.streetNumber);
  187. //
  188. // NSString *detailString = [NSString stringWithFormat:@"%@%@%@",result.addressDetail.district,
  189. // result.addressDetail.streetName,
  190. // result.addressDetail.streetNumber];
  191. //
  192. // // KVC
  193. // [_userLocation setValue:result.addressDetail.province forKeyPath:@"provinceString"];
  194. // [_userLocation setValue:result.addressDetail.city forKeyPath:@"cityString"];
  195. // [_userLocation setValue:result.addressDetail.district forKeyPath:@"districtString"];
  196. // [_userLocation setValue:detailString forKeyPath:@"detailString"];
  197. // }
  198. // _userLocation.error = nil;
  199. // [self p_runBlock];
  200. //}
  201. - (void)p_runBlock {
  202. RDS_WEAKSELF(weakSelf)
  203. if (_rdsLocationCompleted) {
  204. _rdsLocationCompleted(weakSelf.userLocation);
  205. // _geocodesearch.delegate = nil;
  206. }
  207. }
  208. @end