Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created November 4, 2014 01:51
Show Gist options
  • Select an option

  • Save jebai0521/7715b41d454cf8ece456 to your computer and use it in GitHub Desktop.

Select an option

Save jebai0521/7715b41d454cf8ece456 to your computer and use it in GitHub Desktop.
@interface LocationMgr () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager* locationManager;
@property (strong, nonatomic) CLGeocoder* geocoder;
@property (assign, nonatomic, getter=isRunning) BOOL running;
@end
@implementation LocationMgr
- (id) init
{
self = [super init];
if (self) {
_geocoder = [[CLGeocoder alloc] init];
_locationManager = [[CLLocationManager alloc]init];
[_locationManager requestWhenInUseAuthorization];
//设置代理
_locationManager.delegate = self;
//设置位置精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置每隔100米更新位置
_locationManager.distanceFilter = 100;
}
return self;
}
+ (id) shareInstance
{
static dispatch_once_t onceToken;
static LocationMgr* mgr;
dispatch_once(&onceToken, ^{
mgr = self.new;
});
return mgr;
}
/**
* <#Description#>
*
* @param completeBlock 任务完成的回调
*/
- (void)currentLocationInfo:(LocationMgrTaskCompleteBlock)completeBlock error:(NSError *__autoreleasing *)error
{
if ([self isRunning]) {
NSLog(@"LocationMgr is Busy......");
if (error) {
*error = [NSError errorWithDomain:@"LocationMgr" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"定位服务繁忙,请稍后再试", @"message", nil]];
}
return;
}
_running = YES;
if ([CLLocationManager authorizationStatus] < kCLAuthorizationStatusAuthorized ) {
if (error) {
*error = [NSError errorWithDomain:@"LocationMgr" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"定位服务未授权,请在设置中开启授权", @"message", nil]];
}
}
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];
_completeBlock = completeBlock;
}
/*
* locationManager:didUpdateToLocation:fromLocation:
*
* Discussion:
* Invoked when a new location is available. oldLocation may be nil if there is no previous location
* available.
*
* This method is deprecated. If locationManager:didUpdateLocations: is
* implemented, this method will not be called.
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[_geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil &&[placemarks count] > 0){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
/* We received the results */
NSLog(@"Country = %@", placemark.country);
NSLog(@"Postal Code = %@", placemark.postalCode);
NSLog(@"Locality = %@", placemark.locality);
NSLog(@"dic = %@", placemark.addressDictionary );
NSLog(@"dic FormattedAddressLines= %@", [placemark.addressDictionary objectForKey:@"FormattedAddressLines"]);
NSLog(@"dic Name = %@", [placemark.addressDictionary objectForKey:@"Name"]);
NSLog(@"dic State = %@", [placemark.addressDictionary objectForKey:@"State"]);
NSLog(@"dic Street = %@", [placemark.addressDictionary objectForKey:@"Street"]);
NSLog(@"dic SubLocality= %@", [placemark.addressDictionary objectForKey:@"SubLocality"]);
NSLog(@"dic SubThoroughfare= %@", [placemark.addressDictionary objectForKey:@"SubThoroughfare"]);
NSLog(@"dic Thoroughfare = %@", [placemark.addressDictionary objectForKey:@"Thoroughfare"]);
if (_completeBlock) {
_completeBlock(@{@"country":placemark.country});
}
[_locationManager stopUpdatingLocation];
}
else if (error == nil &&
[placemarks count] == 0){
NSLog(@"No results were returned.");
}
else if (error != nil){
NSLog(@"An error occurred = %@", error);
}
_running = NO;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment