美文网首页
无标题文章

无标题文章

作者: 227f600b867c | 来源:发表于2016-08-01 15:22 被阅读13次
#import "XLLocationManager.h"
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface XLLocationManager () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, copy) NSString * currentCity;

@end

@implementation XLLocationManager

- (void)startLocate {
    //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
       //移动一百米时重新定位,如果不设置该参数,则有可能会多次调用代理函数 didUpdateLocations
        self.locationManager.distanceFilter = 100;
        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [self.locationManager requestWhenInUseAuthorization];
        }
        [self.locationManager startUpdatingLocation];
    }
}

- (void)uploadLocation:(XLLocation *)location complete:(void (^)(BOOL, id))complete {
    
}

#pragma mark - CLLocationManagerDelegate

//定位失败则执行此代理方法
//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开定位设置
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:ok];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];

}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [self.locationManager stopUpdatingLocation];
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    CLLocationCoordinate2D coordinate = currentLocation.coordinate;

    //反编码
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            self.currentCity = placeMark.locality;
            //直辖市
            if (!self.currentCity) {
                self.currentCity = placeMark.administrativeArea;
            }
            XLLocation *result = [[XLLocation alloc] init];
            result.city = self.currentCity;
            result.latitude = [NSString stringWithFormat:@"%f",coordinate.latitude];
            result.longitude = [NSString stringWithFormat:@"%f",coordinate.longitude];
            if (self.delegate && [self.delegate respondsToSelector:@selector(locatedResult:)]) {
                [self.delegate locatedResult:result];
            }
//            NSLog(@"%@",self.currentCity);
//            NSLog(@"%@",placeMark.name);
            //地址名
            NSLog(@"name:%@",placeMark.name);
            //街道
            NSLog(@"thoroughfare:%@",placeMark.thoroughfare);
            //区
            NSLog(@"subLocality:%@",placeMark.subLocality);
            //市
            NSLog(@"locality:%@",placeMark.locality);
            //省
            NSLog(@"administrativeArea:%@",placeMark.administrativeArea);
            //国家
            NSLog(@"country:%@",placeMark.country);
            NSLog(@"%f, %f",coordinate.longitude, coordinate.latitude);
        } else if (error == nil && placemarks.count == 0) {
            NSLog(@"No location and error return");
        } else if (error) {
            NSLog(@"location error: %@ ",error);
        }
    }];
}
@end

相关文章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章 无标题文章 无标题文章无标题文章 无标题文章 无标题文章

网友评论

      本文标题:无标题文章

      本文链接:https://www.haomeiwen.com/subject/yynxsttx.html