123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // LCLocationManager.m
- // LiveChat
- //
- // Created by 张灿 on 2018/4/21.
- // Copyright © 2018年 caicai. All rights reserved.
- //
- #import "LCLocationManager.h"
- @interface LCLocationManager ()<CLLocationManagerDelegate>
- @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<CLLocation *> *)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<CLPlacemark *> * _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
|