美文网首页
获取当前地理位置

获取当前地理位置

作者: BeSt2wazi | 来源:发表于2017-08-03 16:36 被阅读0次
#import <Foundation/Foundation.h>

@interface LBLocationCenter : NSObject

@property(nonatomic,strong) NSString *provinceName;
@property(nonatomic,strong) NSString *cityName;
@property(nonatomic,strong) NSString *distictName;
@property(nonatomic,strong) NSString *cityId;

+ (LBLocationCenter *)shareInstance;

+ (BOOL)enableLocation;

- (void)startLocation;
- (void)stopLocation;

@end
#import "LBLocationCenter.h"
#import <CoreLocation/CoreLocation.h>

static LBLocationCenter *stat_loc = nil;

@interface LBLocationCenter()
<
CLLocationManagerDelegate
>{
    CLLocationManager *_locationManager;
}

@end

@implementation LBLocationCenter

+ (LBLocationCenter *)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        stat_loc = [[LBLocationCenter alloc] init];
    });
    return stat_loc;
}

- (id)init {
    if (self = [super init]) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return self;
}

+ (BOOL)enableLocation {
    if ([CLLocationManager locationServicesEnabled]  //确定用户的位置服务启用
        &&[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        return YES;
    }
    return NO;
}

- (void)startLocation {
    [_locationManager requestWhenInUseAuthorization];
    
    [_locationManager startUpdatingLocation];
}

- (void)stopLocation {
    [_locationManager stopUpdatingLocation];
}

#pragma mark - Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    // 1.获取用户位置的对象
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    LBLog(@"当前位置 纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    [geocoder reverseGeocodeLocation:loc
                   completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
     {
         CLPlacemark *placeModel = [placemarks firstObject];
         
         LBLog(@"-------%@_%@_%@_%@_%@",
               placeModel.country,
               placeModel.administrativeArea,
               placeModel.subAdministrativeArea,
               placeModel.locality,
               placeModel.subLocality);
         
         if (placeModel) {
             self.provinceName = [NSString getSafeString:placeModel.administrativeArea];
             if ([self.provinceName length] == 0) {
                 self.provinceName = [NSString getSafeString:placeModel.subAdministrativeArea];
             }
             self.cityName = [NSString getSafeString:placeModel.locality];
             self.distictName = [NSString getSafeString:placeModel.subLocality];
         }
     }];
    
    // 2.停止定位
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusNotDetermined) {
        [self startLocation];
    }
    LBLog(@"---定位权限 改变 : %d",status);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if (error.code == kCLErrorDenied) {
        //没有权限
        LBLog(@"----无权限使用定位");
    }
    LBLog(@"-----定位失败 %@",error);
}

@end

相关文章

网友评论

      本文标题:获取当前地理位置

      本文链接:https://www.haomeiwen.com/subject/sgvplxtx.html