高德定位

#import

@interface TBPositioning : NSObject

+ (instancetype)sharePosition;

- (void)locateAction:(void (^)(CLLocation *location,AMapLocationReGeocode *regeocode))finished;

@end
@property (nonatomic, strong) AMapLocationManager *locationManager;
static TBPositioning *position = nil;

+ (instancetype)sharePosition
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        position = [[self alloc]init];
        
        [position configLocationManager];
    });
    
    return position;
}

- (void)configLocationManager
{
    self.locationManager = [[AMapLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    [self.locationManager setLocationTimeout:6];
    
    [self.locationManager setReGeocodeTimeout:3];
}

- (void)locateAction:(void (^)(CLLocation *location,AMapLocationReGeocode *regeocode))finished
{
    WS(weakSelf);
    //带逆地理的单次定位
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        if (error)
        {
            DLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        
        //定位信息
        DLog(@"location------:%@", location);
        DLog(@"reGeocode-----:%@", regeocode.city);
        //逆地理信息
        if (regeocode)
        {
            finished(location,regeocode);
            [weakSelf cleanUpAction];
        }
    }];
}
- (void)cleanUpAction
{
    //停止定位
    [self.locationManager stopUpdatingLocation];
    
    [self.locationManager setDelegate:nil];
    
}

你可能感兴趣的:(高德定位)