为了更好的活着

iOS 工具类 -- 定位

2016-11-13  本文已影响205人  哎呀我Qu

一、准备

1. Frameworks

添加 CoreLocation.framework 到工程中

2. Info.plist

配置 Info.plist

  1. 将 Info.plist 以 source code 形式打开,粘贴如下代码(根据需求看具体用那条,都粘上也没坏处)
    <key>NSLocationUsageDescription</key>
    <string>App需要您的同意,才能访问位置</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>App需要您的同意,才能始终访问位置</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>App需要您的同意,才能在使用期间访问位置</string>
    key 一定不能写错,string 可以随便写

2.或者直接在列表下直接添加 NSLocationUsageDescription 为 Key,String 为 Type,Value 中看情况写

⚠️ iOS 10 之后的权限,如相机、麦克风等,需在 Info.plist 中进行配置

二、思路

1. 创建单例,并在 init 方法中初始化
2. 定义 block 及 getGps 方法
3. 调用 startUpdatingLocation 并通过 CLLocationCoordinate2D 获得经纬度

三、代码

1. QPLocationManager.h
#import <Foundation/Foundation.h>

typedef void(^LocationBlock)(NSString * lon, NSString * lat);

@interface QPLocationManager : NSObject

+ (instancetype)sharedManager;

- (void)getGps:(LocationBlock)block;

@property (nonatomic, strong) NSString * lon;
@property (nonatomic, strong) NSString * lat;

@end
2. QPLocationManager.m
#import "QPLocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface QPLocationManager ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager * locManager;

@property (nonatomic, copy) LocationBlock block;

@end

@implementation QPLocationManager

+ (instancetype)sharedManager {
    
    static QPLocationManager * _manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[QPLocationManager alloc] init];
    });
    
    return _manager;
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locManager.distanceFilter = 100;
        _locManager.delegate = self;
        
        if (![CLLocationManager locationServicesEnabled]) {
            NSLog(@"请开启定位服务");
        } else {
            
            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

            if (status == kCLAuthorizationStatusNotDetermined) {
                [_locManager requestWhenInUseAuthorization];
            }
            
        }
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    
    CLLocationCoordinate2D coor = newLocation.coordinate;
    
    NSString * lat = [NSString stringWithFormat:@"%@",@(coor.longitude)];
    NSString * lon = [NSString stringWithFormat:@"%@",@(coor.latitude)];
    
    [QPLocationManager sharedManager].lon = lon;
    [QPLocationManager sharedManager].lat = lat;
   
    self.block(lat,lon);
    
    [self.locManager stopUpdatingLocation];
  
}

- (void)getGps:(LocationBlock)block {
    
    self.block = block;
    [self.locManager startUpdatingLocation];
    
}

@end
3. 调用(在你需要的地方,引入头文件)
[[QPLocationManager sharedManager] getGps:^(NSString *lat, NSString *lon) {
        
    NSLog(@"%@,%@",lon, lat);
        
}];

上一篇 下一篇

猜你喜欢

热点阅读