@implementation IFLocationManager
@synthesize locationServiceFinished = _locationServiceFinished;
@synthesize locationServiceFailed = _locationServiceFailed;
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
#pragma mark -
#pragma mark - 单例方法
+ (instancetype)manager
{
static IFLocationManager *core = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
core = [IFLocationManager new];
});
return core;
}
#pragma mark -
#pragma mark - 启动定位服务
- (void)startLocation
{
if (locationManager) {
locationManager.delegate = nil;
locationManager = nil;
}
if ([CLLocationManager locationServicesEnabled]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
//不需要太高精度,费电
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
}
#pragma mark -
#pragma mark - 停止定位服务
- (void)stopLocation
{
[locationManager stopUpdatingLocation];
locationManager = nil;
}
#pragma mark -
#pragma mark - 定位服务 - 更新地理位置回调方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * newLocation = [locations lastObject];//最后一次位置是当前位置
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
{
for (CLPlacemark *placeMark in placemarks)
{
NSDictionary *addressDic = placeMark.addressDictionary;
__unused NSString *state = [addressDic objectForKey:@"State"];
__unused NSString *city = [addressDic objectForKey:@"City"];
__unused NSString *subLocality = [addressDic objectForKey:@"SubLocality"];
__unused NSString *street = [addressDic objectForKey:@"Street"];
__unused NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
__unused NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
if (_locationServiceFinished) {
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
state,LocationValue_State,
city,LocationValue_City,
subLocality,LocationValue_SubLocality,
street,LocationValue_Street,
latitude,LocationValue_Latitude,
longitude,LocationValue_Longitude,
placeMark.administrativeArea,LocationValue_AdministrativeArea,
nil];
_locationServiceFinished(values);
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"12345" message:@"s" delegate:nil cancelButtonTitle:@"c" otherButtonTitles:nil, nil];
[a show];
}
[self stopLocation];
}
};
[clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
}
#pragma mark -
#pragma mark - 定位服务 - 位置授权状态变化回调方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined :
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//iOS8新增 需要在info.plist中添加字段NSLocationAlwaysUsageDescription,值为空即可
[locationManager requestAlwaysAuthorization];
}
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
//iOS8新增 需要在info.plist中添加字段NSLocationWhenInUseUsageDescription,值为空即可
[locationManager requestWhenInUseAuthorization];
}
}
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied :
NSLog(@"kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
break;
default:
break;
}
}
#pragma mark -
#pragma mark - 定位服务 - 获取地理位置数据失败回调方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error) {
if (error.code==1){
//不允许定位
// 定位服务没有得到您的授权,默认定位北京!
if(_locationServiceFailed){
_locationServiceFailed(LocationServiceError_NOAuthorize);
}
}else{
//定位失败,默认定位北京!
if(_locationServiceFailed){
_locationServiceFailed(LocationServiceError_LocationError);
}
}
}
[self stopLocation];
}
@end
iOS Location Service
最新推荐文章于 2023-01-11 14:24:46 发布