// 在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);
}
iOS定位服务 CoreLocation
最新推荐文章于 2021-07-26 15:04:48 发布