iOS定位服务 CoreLocation
2016-04-05 16:59
429 查看
// 在iOS中,定位服务API主要使用CoreLocation框架, /* CLLocationManager 用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以或得设备的运行方向等。 CLLocationManagerDelegate CLLocationManager 类的委托协议。 CLLocation 改类封装了位置和高度信息 打开定位服务:设置->隐私->定位服务->XX 应用 */ // 1、导入CoreLocation 框架 #import <CoreLocation/CoreLocation.h> // 2、遵守 协议 @interface OnePositionViewController ()<CLLocationManagerDelegate> { // 3、定义了CLLocationManager 属性 CLLocationManager *_manager; CLGeocoder *_geocoder; } @property (nonatomic,strong)CLGeocoder *geocoder; @property (weak, nonatomic) IBOutlet UILabel *latLabel; @property (weak, nonatomic) IBOutlet UILabel *lngLabel; @property (weak, nonatomic) IBOutlet UILabel *altLabel; @property (weak, nonatomic) IBOutlet UILabel *locationLabel; @end @implementation OnePositionViewController -(CLGeocoder *)geocoder{ if (!_geocoder) { _geocoder = [[CLGeocoder alloc] init]; } return _geocoder; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"地理定位和地理信息反编码"; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"用户关闭了定位"); return; } // 创建并初始化locationManager 属性, _manager = [[CLLocationManager alloc] init]; _manager.delegate = self; // 精确度 精确度越高,请求获得位置信息的时间就越短,设备越耗电 /* kCLLocationAccuracyBestForNavigation 导航情况下最高的精确度,一般有外接电源时才能使用 (车载导航) kCLLocationAccuracyBest; 设备使用电池供应时最高的精确度。 kCLLocationAccuracyNearestTenMeters; 精确到10米 kCLLocationAccuracyHundredMeters; 精确到100米 (徒步旅行者的应用) kCLLocationAccuracyKilometer; 精确到1000米 kCLLocationAccuracyThreeKilometers; 精确到3000米 */ _manager.desiredAccuracy = kCLLocationAccuracyBest; // 设置距离过滤器,定义了设备移动后获得位置信息的最小距离。单位是米 _manager.distanceFilter = 1000.0f; // 弹出用户授权提示框 修改工程配置文件 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription. if ([_manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_manager requestAlwaysAuthorization]; } // 开始定位,也可以放在 viewWillAppear 中调用,在 [_manager startUpdatingLocation]; } //- (void)viewWillAppear:(BOOL)animated{ // [super viewWillAppear:animated]; // // // 开始定位 // [_manager startUpdatingLocation]; //} // //- (void)viewWillDisappear:(BOOL)animated{ // [super viewDidAppear:animated]; // // 停止定位 // [_manager stopUpdatingLocation]; //} #pragma mark -------- 授权状态发生变化时调用 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined: if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [manager requestWhenInUseAuthorization]; } break; default: break; } } #pragma mark - ------------ 定位成功 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ // NSLog(@"locations = %@",locations); CLLocation *location = locations.lastObject; NSLog(@"经度:%f",location.coordinate.latitude); NSLog(@"纬度:%f",location.coordinate.longitude); NSLog(@"高度:%f",location.altitude); self.latLabel.text = [NSString stringWithFormat:@"经度:%f",location.coordinate.latitude]; self.lngLabel.text = [NSString stringWithFormat:@"纬度:%f",location.coordinate.longitude]; self.altLabel.text = [NSString stringWithFormat:@"高度:%f",location.altitude]; // location.coordinate.latitude // location.coordinate.longitude //根据经纬度解析地理位置 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (!error) { CLPlacemark *placemark = placemarks[0]; NSLog(@"address = %@",placemark.addressDictionary); NSDictionary *addressDictionary = placemark.addressDictionary; // 国家 NSString *address = addressDictionary[@"Country"]; // 省份 NSString *addressState = addressDictionary[@"State"]; // 城市 NSString *addressCity = addressDictionary[@"City"]; // 区域 NSString *addressSubLocality = addressDictionary[@"SubLocality"]; // 街道 NSString *addressThoroughfare = addressDictionary[@"Thoroughfare"]; self.locationLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@ %@",address,addressState,addressCity,addressSubLocality,addressThoroughfare]; // // for (NSString *key in placemark.addressDictionary.allKeys) { // NSLog(@"%@:%@",key,placemark.addressDictionary[key]); // } [_manager stopUpdatingLocation]; }else{ NSLog(@"error:%@",error); } }]; } #pragma mark ------- 定位失败 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"error :%@",error); }
相关文章推荐
- 解决在低版本的xcode上使用高版本iOS系统手机进行真机测试
- iOS中二维码,条形码等的扫描
- iOS9 联系人 ContactFramework
- 定位功能iOS SDK
- IOS用CGContextRef画各种图形
- iOS各大名人的博客
- iOS 更新约束
- 谈谈iOS 的加密技术
- iOS开发-NSURLCache(缓存)
- iOS项目开发中 webView 服务器返回来一堆不标准的html 语言 处理办法
- iOS--项目目录组织及生成脚本
- iOS 16进制与10进制的互转
- iOS--苹果设备
- iOS之两个ImageView实现图片滚动
- iOS-Bug收集:CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
- FFmpeg在iOS上完美编译
- iOS--独立开发的相关
- Initialization of variable was never used; consider replacing with assignment to ‘_’ or removing it
- iOS NSSet和NSMutableSet
- iOS - 正则表达式判断邮箱、手机号码验证、车牌号验证、车型、用户名、密码、昵称、身份证号