// // LCLocationManager.m // LiveChat // // Created by 张灿 on 2018/4/21. // Copyright © 2018年 caicai. All rights reserved. // #import "LCLocationManager.h" @interface LCLocationManager () @property(nonatomic,copy)locationBlock block; @end @implementation LCLocationManager +(instancetype)shareInstance{ static LCLocationManager* shareInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shareInstance = [[super allocWithZone:NULL]init]; }); return shareInstance; } + (instancetype)allocWithZone:(struct _NSZone *)zone{ return [LCLocationManager shareInstance]; } -(id)copyWithZone:(struct _NSZone *)zone { return [LCLocationManager shareInstance]; } - (void)getLocationCity:(locationBlock)locationblock{ self.location = nil; self.location = [[CLLocationManager alloc]init]; self.location.delegate = self; self.block = locationblock; #if TARGET_IPHONE_SIMULATOR//模拟器 self.block(@"");//用户未授权 self.block = nil; #elif TARGET_OS_IPHONE//真机 if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) { if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) { [self.location requestWhenInUseAuthorization]; } [self.location startUpdatingLocation]; }else { self.block(@"");//用户未授权 self.block = nil; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请在手机系统设置中允许花蝶在使用期间访问你的地理位置,我们才能更好的为你推荐附近的人" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){ }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [[ZCUtils getCurrentVC] presentViewController:alertController animated:YES completion:nil]; } #endif } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ self.location = nil; CLLocation *currentLocation = [locations lastObject]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude); //反编码 __block NSString* currentCity; [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) { if (placemarks.count > 0) { CLPlacemark *placeMark = placemarks[0]; currentCity = placeMark.locality; NSLog(@"地址地址===%@--%@--%@--%@--%@--%@--%@--%@--%@--%@--%@--%@--%@",placeMark.name,placeMark.thoroughfare,placeMark.subThoroughfare,placeMark.locality,placeMark.subLocality,placeMark.administrativeArea,placeMark.subAdministrativeArea,placeMark.postalCode,placeMark.ISOcountryCode,placeMark.country,placeMark.inlandWater,placeMark.ocean,[placeMark.areasOfInterest firstObject]); NSDictionary *cityDict = [LCSaveData getCityDict]; // NSString *key = [cityDict k] [cityDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if ([[NSString stringWithFormat:@"%@市",obj] isEqualToString:currentCity]) { NSLog(@"----------%@",key); } }]; if (self.block && currentCity) { self.block(currentCity); self.block = nil; } } }]; [self.location stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { self.block(@""); NSLog(@"%@",error.description); [self.location stopUpdatingLocation]; } @end