// // RDSMapHelper.m // SmartLightForBigFish // // Created by coderYK on 2017/9/11. // Copyright © 2017年 RD. All rights reserved. // #import "RDSMapHelper.h" #import //#import // 百度地图库 #import // 苹果自动地图库 @interface RDSMapHelper () { // BMKLocationService *_locService; // BMKGeoCodeSearch* _geocodesearch; // BOOL bGeoCodeSearch,bConfiguration; float _latitude; float _longitude; } @property (strong,nonatomic) CLLocationManager* locationManager; @property (nonatomic, copy) void(^rdsLocationCompleted)(RDSUserLocation *userLocation); // 定位成功的回调 @end @implementation RDSMapHelper + (instancetype)shareHelper { static RDSMapHelper *bmHelper; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ bmHelper = [[RDSMapHelper alloc] init]; }); return bmHelper; } - (void)rds_startLocationSevice:(void (^)(RDSUserLocation *))rdsLocationCompleted { _rdsLocationCompleted = rdsLocationCompleted; // RDS_WEAKSELF(weakSelf) // dispatch_async(dispatch_get_main_queue(), ^{ //[weakSelf p_setupBaiduMap]; [self p_setupLocationMap]; // }); } #pragma mark - 苹果地图 - (void)p_setupLocationMap{ if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) { BLOCK_SAFE_RUN(_rdsLocationCompleted, nil) return ; } if(!_locationManager){ self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 100.0f; _userLocation = [[RDSUserLocation alloc] init]; } if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){ [self.locationManager requestWhenInUseAuthorization]; } if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // _locationManager.allowsBackgroundLocationUpdates =YES; } [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined: if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } break; default: break; } } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *newLocation = locations[0]; CLLocationCoordinate2D oldCoordinate = newLocation.coordinate; NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude); [manager stopUpdatingLocation]; RDS_WEAKSELF(weakSelf) CLGeocoder *geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *_Nullable placemarks, NSError * _Nullable error) { for (CLPlacemark *place in placemarks) { NSLog(@"国家,%@",place.country); // 国家 NSLog(@"省份,%@",place.administrativeArea); // 省份 NSLog(@"城市,%@",place.locality); // 城市 NSLog(@"区,%@",place.subLocality); // 区 NSLog(@"位置名,%@",place.name); // 位置名 NSLog(@"thoroughfare,%@",place.thoroughfare); // 街道 NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道 // KVC [weakSelf.userLocation setValue:place.administrativeArea forKeyPath:@"provinceString"]; [weakSelf.userLocation setValue:place.locality forKeyPath:@"cityString"]; [weakSelf.userLocation setValue:place.subLocality forKeyPath:@"districtString"]; [weakSelf.userLocation setValue:place.name forKeyPath:@"detailString"]; if (kNULLString(place.locality)) { NSLog(@"定位失败"); } } weakSelf.userLocation.error = nil; [weakSelf p_runBlock]; }]; } #pragma mark - 百度地图 //- (void)p_setupBaiduMap { // // if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || // [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) { // BLOCK_SAFE_RUN(_rdsLocationCompleted, nil) // return ; // } // // // 要使用百度地图,请先启动BaiduMapManager // if (!bConfiguration) { // // BMKMapManager *mapManager = [[BMKMapManager alloc]init]; // bConfiguration = [mapManager start:@"BaiduAppKey" generalDelegate:nil]; // } // // if (!bConfiguration) { // DDLog(@"manager start failed!"); // } // // //设置定位精确度,默认:kCLLocationAccuracyBest // [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; // //指定最小距离更新(米),默认:kCLDistanceFilterNone // [BMKLocationService setLocationDistanceFilter:100.f]; // // //初始化BMKLocationService // _locService = [[BMKLocationService alloc]init]; // _locService.delegate = self; // _userLocation = [[RDSUserLocation alloc] init]; // // _geocodesearch = [[BMKGeoCodeSearch alloc] init]; // _geocodesearch.delegate = self; // // //启动LocationService // [_locService startUserLocationService]; //} // //#pragma mark - 百度地图 代理 // ///** // *用户位置更新后,会调用此函数 // *@param userLocation 新的用户位置 // */ //- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation //{ // DDLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude, // userLocation.location.coordinate.longitude); // // _latitude = userLocation.location.coordinate.latitude; // _longitude = userLocation.location.coordinate.longitude; // // [_userLocation setValue:@(_latitude) forKeyPath:@"latitude"]; // [_userLocation setValue:@(_longitude) forKeyPath:@"longitude"]; // // CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0}; // if (_latitude != 0 && _longitude != 0) { // pt = (CLLocationCoordinate2D){_latitude, _longitude}; // } // BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; // reverseGeocodeSearchOption.reverseGeoPoint = pt; // bGeoCodeSearch = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; // if(bGeoCodeSearch) // { // DDLog(@"反geo检索发送成功"); // } // else // { // DDLog(@"反geo检索发送失败"); // _userLocation.error = [NSError errorWithDomain:@"反geo检索发送失败" code:-1 userInfo:nil]; // [self p_runBlock]; // } // // [_locService stopUserLocationService]; //} // // //- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher // result:(BMKReverseGeoCodeResult *)result // errorCode:(BMKSearchErrorCode)error { // // if (error == 0) { // DDLog(@"~~~result:%@,%@,%@,%@%@",result.addressDetail.province, // result.addressDetail.city, // result.addressDetail.district, // result.addressDetail.streetName, // result.addressDetail.streetNumber); // // NSString *detailString = [NSString stringWithFormat:@"%@%@%@",result.addressDetail.district, // result.addressDetail.streetName, // result.addressDetail.streetNumber]; // // // KVC // [_userLocation setValue:result.addressDetail.province forKeyPath:@"provinceString"]; // [_userLocation setValue:result.addressDetail.city forKeyPath:@"cityString"]; // [_userLocation setValue:result.addressDetail.district forKeyPath:@"districtString"]; // [_userLocation setValue:detailString forKeyPath:@"detailString"]; // } // _userLocation.error = nil; // [self p_runBlock]; //} - (void)p_runBlock { RDS_WEAKSELF(weakSelf) if (_rdsLocationCompleted) { _rdsLocationCompleted(weakSelf.userLocation); // _geocodesearch.delegate = nil; } } @end