欢迎访问快速建站网站我们提供建站工具系统

苹果系统CLLocationManager获取定位及根据位置获取经纬度

作者:jcmp      发布时间:2021-04-18      浏览量:0
一、CLLocationManager获

一、CLLocationManager获取定位

#import #import typedef void (^locationInfoBlock) (CLPlacemark *mark, CLLocation *location);typedef void (^coordinate2DBlock) (NSNumber *latitude, NSNumber *longitude);@interface SmartCLLocationManager : NSObject+ (void)startLocation:(locationInfoBlock)location;@end。

#import "SmartCLLocationManager.h"#import "SmartToast.h"#import "Util.h"@interface SmartCLLocationManager (){ CLLocationManager *_locationmanager; locationInfoBlock locationBlock;}@end@implementation SmartCLLocationManager+(instancetype)shareManager{ static SmartCLLocationManager *manager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager = [[SmartCLLocationManager alloc] init]; }); return manager;}-(instancetype)init{ if (self = [super init]) { [self initLocationManager]; } return self;}+ (void)startLocation:(locationInfoBlock)location{ [[SmartCLLocationManager shareManager] startLocation:location];}- (void)startLocation:(locationInfoBlock)location{ if (![CLLocationManager locationServicesEnabled]) { [self showAlert]; return; } locationBlock = [location copy]; [_locationmanager stopUpdatingLocation];//停止上次定位 [_locationmanager startUpdatingLocation];//开始新的定位 }- (void)initLocationManager{ if ([CLLocationManager locationServicesEnabled]) { _locationmanager = [[CLLocationManager alloc] init]; _locationmanager.delegate = self; if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0) { [_locationmanager requestWhenInUseAuthorization];//前台定位// [_locationmanager requestAlwaysAuthorization];//前后台同时定位 } _locationmanager.desiredAccuracy = kCLLocationAccuracyKilometer;//kCLLocationAccuracyBest比较耗性能 _locationmanager.distanceFilter = 5.0; }else{ [self showAlert]; }}#pragma mark --- CLLocationManagerDelegate-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations//定位成功{ CLLocation *currentLocation = [locations lastObject]; CLGeocoder *geoCoder = [[CLGeocoder alloc]init]; //打印当前的经度与纬度 DLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude); //反地理编码 [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) { if (placemarks.count > 0) { CLPlacemark *placeMark = placemarks[0]; NSString *currentCity = placeMark.locality; if (locationBlock) { locationBlock(placeMark,currentLocation); } /*看需求定义一个全局变量来接收赋值*/ DLog(@"----%@",placeMark.country);//当前国家 DLog(@"%@",currentCity);//当前的城市 DLog(@"%@",placeMark.subLocality);//当前的位置 DLog(@"%@",placeMark.thoroughfare);//当前街道 DLog(@"%@",placeMark.name);//具体地址 }else if (error == nil && placemarks.count == 0){ DLog(@"没有定位信息"); }else if (error != nil){ DLog(@"error"); } }]; [_locationmanager stopUpdatingHeading];}-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error//定位失败{ if (locationBlock){ locationBlock(nil,nil); } [_locationmanager stopUpdatingLocation]; if ([error code] == kCLErrorDenied) {//没有授权 [self showAlert]; } else if ([error code] == kCLErrorLocationUnknown){ [SmartToast Tost:@"定位数据不可用"]; } else{ [SmartToast Tost:@"未知错误,定位失败"]; }}-(void)showAlert{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [Util jumpToWifiSetting]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:okAction]; [alert addAction:cancelAction]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];}

二、根据位置获取经纬度

+ (void)converAdress:(NSString *)address ToCoordinate2D:(coordinate2DBlock)coordinate;//根据传入的地理位置-->经纬度。

+ (void)converAdress:(NSString *)address ToCoordinate2D:(coordinate2DBlock)coordinate//根据传入的地理位置-->经纬度{ CLGeocoder *ge = [[CLGeocoder alloc] init]; [ge geocodeAddressString:address completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) { if ([placemarks count] > 0 && error == nil) { CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; NSNumber *latitude = [NSNumber numberWithDouble:firstPlacemark.location.coordinate.latitude]; NSNumber *longitude = [NSNumber numberWithDouble:firstPlacemark.location.coordinate.longitude]; coordinate(latitude,longitude); }else if ([placemarks count] == 0 && error == nil) { DLog(@"Found no placemarks."); coordinate(nil,nil); }else if (error != nil) { DLog(@"An error occurred = %@", error); coordinate(nil,nil); } }];}