LCLocationManager.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // LCLocationManager.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/4/21.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "LCLocationManager.h"
  9. @interface LCLocationManager ()<CLLocationManagerDelegate>
  10. @property(nonatomic,copy)locationBlock block;
  11. @end
  12. @implementation LCLocationManager
  13. +(instancetype)shareInstance{
  14. static LCLocationManager* shareInstance = nil;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. shareInstance = [[super allocWithZone:NULL]init];
  18. });
  19. return shareInstance;
  20. }
  21. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  22. return [LCLocationManager shareInstance];
  23. }
  24. -(id)copyWithZone:(struct _NSZone *)zone
  25. {
  26. return [LCLocationManager shareInstance];
  27. }
  28. - (void)getLocationCity:(locationBlock)locationblock{
  29. self.location = nil;
  30. self.location = [[CLLocationManager alloc]init];
  31. self.location.delegate = self;
  32. self.block = locationblock;
  33. #if TARGET_IPHONE_SIMULATOR//模拟器
  34. self.block(@"");//用户未授权
  35. self.block = nil;
  36. #elif TARGET_OS_IPHONE//真机
  37. if ([CLLocationManager locationServicesEnabled] &&
  38. ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse
  39. || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
  40. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
  41. [self.location requestWhenInUseAuthorization];
  42. }
  43. [self.location startUpdatingLocation];
  44. }else {
  45. self.block(@"");//用户未授权
  46. self.block = nil;
  47. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请在手机系统设置中允许花蝶在使用期间访问你的地理位置,我们才能更好的为你推荐附近的人" preferredStyle:UIAlertControllerStyleAlert];
  48. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){
  49. }];
  50. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  51. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  52. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  53. [[UIApplication sharedApplication] openURL:url];
  54. }
  55. }];
  56. [alertController addAction:cancelAction];
  57. [alertController addAction:okAction];
  58. [[ZCUtils getCurrentVC] presentViewController:alertController animated:YES completion:nil];
  59. }
  60. #endif
  61. }
  62. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
  63. self.location = nil;
  64. CLLocation *currentLocation = [locations lastObject];
  65. CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
  66. CLLocation *location = [locations lastObject];
  67. CLLocationCoordinate2D coordinate = location.coordinate;
  68. NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
  69. //反编码
  70. __block NSString* currentCity;
  71. [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
  72. if (placemarks.count > 0) {
  73. CLPlacemark *placeMark = placemarks[0];
  74. currentCity = placeMark.locality;
  75. 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]);
  76. NSDictionary *cityDict = [LCSaveData getCityDict];
  77. // NSString *key = [cityDict k]
  78. [cityDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  79. if ([[NSString stringWithFormat:@"%@市",obj] isEqualToString:currentCity]) {
  80. NSLog(@"----------%@",key);
  81. }
  82. }];
  83. if (self.block && currentCity) {
  84. self.block(currentCity);
  85. self.block = nil;
  86. }
  87. }
  88. }];
  89. [self.location stopUpdatingLocation];
  90. }
  91. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  92. self.block(@"");
  93. NSLog(@"%@",error.description);
  94. [self.location stopUpdatingLocation];
  95. }
  96. @end