123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- //
- // RDSMapHelper.m
- // SmartLightForBigFish
- //
- // Created by coderYK on 2017/9/11.
- // Copyright © 2017年 RD. All rights reserved.
- //
- #import "RDSMapHelper.h"
- #import <CoreLocation/CoreLocation.h>
- //#import <BaiduMapAPI/BMapKit.h>// 百度地图库
- #import <MapKit/MapKit.h>// 苹果自动地图库
- @interface RDSMapHelper ()<CLLocationManagerDelegate> {
-
- // 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<CLLocation *> *)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<CLPlacemark *> *_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
|