IOS 定位 CoreLocation的封裝

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>


@interface GpsManager : NSObject <CLLocationManagerDelegate> {
    CLLocationManager *manager;
    
    // block的申明 定義
    void (^saveGpsCallback) (double lat, double lng);
}

// 申明一個 cb的對象
+ (void) getGps:(  void (^)(double lat, double lng) )cb;
+ (void) stop;

@end
#import "GpsManager.h"
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>

@implementation GpsManager

+ (id) sharedGpsManager {
    static id s;
    if (s == nil) {
        s = [[GpsManager alloc] init];
    }
    return s;
}
- (id)init {
    self = [super init];
    if (self) {
        // 打開定位 而後獲得數據
        manager = [[CLLocationManager alloc] init];
        manager.delegate = self;
        manager.desiredAccuracy = kCLLocationAccuracyBest;
        
        // 兼容iOS8.0版本
        /* Info.plist裏面加上2項
         NSLocationAlwaysUsageDescription      Boolean YES
         NSLocationWhenInUseUsageDescription   Boolean YES
         */

        // 請求受權 requestWhenInUseAuthorization用在>=iOS8.0上
        // respondsToSelector: 前面manager是否有後面requestWhenInUseAuthorization方法
        // 1. 適配 動態適配
        if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [manager requestWhenInUseAuthorization];
            [manager requestAlwaysAuthorization];
        }
        // 2. 另一種適配 systemVersion 有多是 8.1.1
        float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (osVersion >= 8) {
            [manager requestWhenInUseAuthorization];
            [manager requestAlwaysAuthorization];
        }
    }
    return self;
}
- (void) getGps:(  void (^)(double lat, double lng) )cb {
    if ([CLLocationManager locationServicesEnabled] == FALSE) {
        return;
    }
    // block通常賦值須要copy
    saveGpsCallback = [cb copy];
    
    // 中止上一次的
    [manager stopUpdatingLocation];
    // 開始新的數據定位
    [manager startUpdatingLocation];
}

+ (void) getGps:(  void (^)(double lat, double lng) )cb {
    [[GpsManager sharedGpsManager] getGps:cb];
}

- (void) stop {
    [manager stopUpdatingLocation];
}
+ (void) stop {
    [[GpsManager sharedGpsManager] stop];
}

// 每隔一段時間就會調用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    for (CLLocation *loc in locations) {
        CLLocationCoordinate2D l = loc.coordinate;
        double lat = l.latitude;
        double lnt = l.longitude;
        
        // 使用blocks 調用blocks
        if (saveGpsCallback) {
            saveGpsCallback(lat, lnt);
        }
    }
}
調用方式:
//只獲取一次定位
    __block  BOOL isOnece = YES;
    [GpsManager getGps:^(double lat, double lng) {
        isOnece = NO;
        
        //只打印一次經緯度
        NSLog(@"lat lng (%f, %f)", lat, lng);
        
        if (!isOnece) {
            [GpsManager stop];
        }
    }];
    
 //若是要一直持續獲取定位則
    [GpsManager getGps:^(double lat, double lng) {
        
        //不斷的打印經緯度
        NSLog(@"lat lng (%f, %f)", lat, lng);
    }];
相關文章
相關標籤/搜索