Core Location
用来确定物理位置,
显示经度, 纬度, 海拔, 水平精度, 垂直精度,
需要打印显示;
或者
创建Label显示. 括弧: 利用代理传值, 有点折腾. 懒得写
和 Map Kit 一样, 都是系统框架.
1 . 创建个Single View, 导入框架, 遵守协议
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
2 . 添加属性变量
@property (nonatomic, strong) CLLocationManager *locationManager;
3 . 准备完成 , 开始
- (void)viewDidLoad {
[super viewDidLoad];
// 位置管理器, 初始化一个对象
self.locationManager = [[CLLocationManager alloc] init];
// 创建的对象, 要遵循CLLocationManagerDelegate协议,作为位置管理器的委托, 当位置信息可用时,调用委托方法
self.locationManager.delegate = self;
// 设置委托后, 设置精度, 注意: 精度高耗电大
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 设置距离筛选器,不用每次更改都通知自己. 因为位置管理器会把检测到的位置更改通知委托,频繁更新会浪费电量
// 偏移1000m后, 通知委托
self.locationManager.distanceFilter = 1000;
// 默认设置没有筛选器
self.locationManager.distanceFilter = kCLHeadingFilterNone;
// 什么时候请求位置信息, iOS8后有变化看注释中的
[self.locationManager requestAlwaysAuthorization];
// 启动位置管理器
[self.locationManager startUpdatingLocation];
// 停止发送更新
[self.locationManager stopUpdatingLocation];
// 将位置信息转换成经纬度
[self.geoCoder geocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *pm = [placemarks firstObject];
CGFloat longitude = pm.location.coordinate.longitude;
CGFloat latitude = pm.location.coordinate.latitude;
NSLog(@" iOS大典 经度 %f, 纬度 %f", longitude, latitude);
}];
}
4 . 代理方法实现
// 定位成功
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations firstObject];
NSLog(@"iOS大典 %@", location);
NSLog(@"iOS大典 %f",location.horizontalAccuracy); // 水平精度
NSLog(@"iOS大典 %f", location.verticalAccuracy); // 垂直精度
NSLog(@"iOS大典 %f", location.coordinate.latitude); // 纬度
NSLog(@"iOS大典 %f", location.coordinate.longitude); // 经度
NSLog(@"iOS大典 %@", location.timestamp); // 时间戳
}
//定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// 错误信息描述
NSLog(@"iOS大典没有错, 错的是%@", [error localizedDescription]);
}
可以 over 了,
不嫌烦的, 就再添加点
在定位成功中添加
// 将位置信息转换成文字
[self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"----%@", [error localizedDescription]);
}else{
CLPlacemark *pm = [placemarks firstObject];
NSLog(@"====%@", pm.addressDictionary);
}
}];
over