打包CLLocationManager定位获取经纬度
封装CLLocationManager定位获取经纬度
创建调用方法
#import <Foundation/Foundation.h> @interface RMMapLocation : NSObject { void (^saveGpsCallBack)(double lattitude,double longitude); } + (void)getGps:(void(^)(double lattitude,double longitude))block; + (void)stop;//停止定位
#import "RMMapLocation.h" @interface RMMapLocation ()<CLLocationManagerDelegate> @property (strong, nonatomic)CLLocationManager *locManager; @end @implementation RMMapLocation + (instancetype)sharedGpsManager { static id mapLocation; <span style="font-family: Menlo; font-size: 13px; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-family: Menlo; font-size: 13px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">static</span><span style="font-family: Menlo; font-size: 13px; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-family: Menlo; font-size: 13px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">dispatch_once_t</span><span style="font-family: Menlo; font-size: 13px; font-variant-ligatures: no-common-ligatures;"> onceToken;</span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #78492a">dispatch_once</span><span style="font-variant-ligatures: no-common-ligatures">(&onceToken, ^{</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span><span style="font-variant-ligatures: no-common-ligatures"> (!mapLocation) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> mapLocation = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">RMMapLocation</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">alloc</span><span style="font-variant-ligatures: no-common-ligatures">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">init</span><span style="font-variant-ligatures: no-common-ligatures">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> });</span></p> return mapLocation; } - (instancetype)init { self = [super init]; if (self) { [self getCurrentLocation]; } return self; } - (void)getCurrentLocation { self.locManager = [[CLLocationManager alloc] init]; self.locManager.delegate = self; self.locManager.desiredAccuracy = kCLLocationAccuracyBest; self.locManager.distanceFilter = 10.0; } - (void)getGps:(void (^)(double lat, double lng))gps { if ([CLLocationManager locationServicesEnabled] == FALSE) { return; } saveGpsCallBack = [gps copy];// block 使用copy进行复制 [self.locManager startUpdatingLocation]; } + (void)getGps:(void (^)(double, double))block { [[RMMapLocation sharedGpsManager] getGps:block]; } - (void)stop { [self.locManager stopUpdatingLocation]; } + (void)stop { [[RMMapLocation sharedGpsManager] stop]; } #pragma mark - locationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { RMLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude); double latitude = newLocation.coordinate.latitude; double longitude = newLocation.coordinate.longitude; if (saveGpsCallBack) { saveGpsCallBack(latitude,longitude); } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { RMLog(@"%@",error); [RMUtils showMessage:@"定位失败"]; }
调用方法(如果只想定位一次调用stop方法即可)
[RMMapLocation getGps:^(double lattitude, double longitude) { RMLog(@"%f----%f",lattitude,longitude); }];